Allow scripts to read a file but prevent users from viewing the file directly

Viewed 11006

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?

7 Answers

Quick hack - rename your file to ".cannotReadFileWithDOT". he server will close reading files with a dot at the beginning of the name, but your scripts will be able to read them. The plus is that the apache and nginx servers out of the box are configured to prohibit reading files with a dot at the beginning of the name.

Related