Lets say I have a plain text file example.txt and I have a PHP script on my web-server readfile.php.
What I want to be able to do is to prevent users from typing http://www.example.com/example.txt and looking at the text file directly but I still want people to be able to load http://www.example.com/readfile.php which reads from the file example.txt and does something with it (possibly displays the contents of the file).
Also, if this can be done, what is the best way to do it?
Yes, this is easy to do.
There are two main ways to stop users from accessing
example.txt. The first is to put it in a folder outside your web folder (Usually calledwwworpublic_html), the second is to put a.htaccessfile in the folder with your example.txt script which blocks access to the file altogether. The.htaccesswould look likeBut you could change
example.txtto something like*.txtif you wanted to block all.txtfiles in the folder.Then you can use
file_get_contents()in yourreadfile.phpto get the contents of the text file, or if you just want to output the file you can usereadfile