2

So I'm making a notepad app in PHP, but I want to add the ability to share the file amongst your peers or something.

It's based on AJAX, and it saves the file automatically, and the file is named to what your IP address is after being hashed in md5.

What I want to do is maybe go to /view/837ec5754f503cfaaee0929fd48974e7, while the actual text file is located at /notes/837ec5754f503cfaaee0929fd48974e7.txt

I know I'll have to use file_get_contents(), but I don't know how to display it on a page.

I could just have it link to the .txt file, but I don't want it raw. I want it to have some style.

How would I go about doing this? Where can I start?

Cody
  • 219
  • 1
  • 11
  • See this http://www.tizag.com/phpT/fileread.php or just do `echo file_get_contents("filename.txt");` - the manual shows you how to do it too http://php.net/manual/en/function.file-get-contents.php – Funk Forty Niner Aug 23 '14 at 02:33
  • How would I assign that to individual files or something? – Cody Aug 23 '14 at 02:34
  • If you want to show multiple files, see http://stackoverflow.com/q/928174/ using `glob` - or do `$var = file_get_contents("filename.txt");` then `echo $var;` ought to do it. – Funk Forty Niner Aug 23 '14 at 02:35
  • 1
    If your URL must look like your question, then you will need to look at `MOD_REWRITE` [link](http://stackoverflow.com/questions/869092/how-to-enable-mod-rewrite-for-apache-2-2). Otherwise, you would need your url to look something like this `/view?file=178634987128976` which has a GET variable `$_GET['file']` with the file name in the url. – Romuloux Aug 23 '14 at 02:36
  • Yeah, `/view?file=2837542837645237` would be absolutely fine. Fred, I only want to show one file on the page, but like how I want the URL. – Cody Aug 23 '14 at 02:38

1 Answers1

3

First you would need a way to store a variable in the URL (the file name). This can be easiest done using the querystring.

So the link to a file for your user to see would be '/view/?file=MYFILENAME'

This would then be interpreted by your php (this could also be wrapped in AJAXy goodness) into a path to retrieve the text file from.

view/index.php

//Fetch the file based on the get variable
//Note the relative path
$file = file_get_contents('../notes/'.$_GET['file'].'.txt');

//Print the file. You can also dress it up or wrap it in HTML tags
echo $file;

When displaying the text file, there is some built in functions that will help. Most notable nl2br() which takes the new line characters in a text file and makes them into html <br> tags.

More reading on the GET array can be found here

Romuloux
  • 689
  • 1
  • 6
  • 21
  • How does `$_GET['file']` work? Does it just find the file of the url that you're visiting? Like, say I went to `/view/?file=837ec5754f503cfaaee0929fd48974e7`, the `$_GET['file']` will default to the 837ec5754f503cfaaee0929fd48974e7 part? – Cody Aug 23 '14 at 02:46
  • Also, could I just make this file view.php and then call a notepad by going to `/view.php?f=837ec5754f503cfaaee0929fd48974e7` ? – Cody Aug 23 '14 at 02:47
  • 1
    `$_GET` is an array of all the parameters passed via the url. So in the url we are saying that the param `file` has a value of `837ec5754f503cfaaee0929fd48974e7`. – Romuloux Aug 23 '14 at 02:50
  • Okay, now I understand. Thank you for your answer. Give me a second to try this out. – Cody Aug 23 '14 at 02:51
  • 1
    Yes, but if the URL is `/view.php?f=MYVAR` the param name would now be `$_GET['f']` because that is what you called it in the URL – Romuloux Aug 23 '14 at 02:51
  • Yep, I just reset the param. Thank you so much! – Cody Aug 23 '14 at 02:52
  • Fantastic! Looks great. – Romuloux Aug 23 '14 at 03:49