tags:

views:

174

answers:

3

Hi ,

I'm using PHP & i wanted to put a text file above(outside) the website root so users can't access it. But i wanted to know how can i read it from my code, i want to open, write/edit some data then save it.

Please give me an example.

Thanks ,

+1  A: 

You just need to use the full path instead of a relative path. To get the directory directly above the document root (Where the website HTML begins) do this:

echo dirname($_SERVER['DOCUMENT_ROOT']);

then, take that value, and use it in your includes/fopens/fgets/file_get_contents

include(dirname($_SERVER['DOCUMENT_ROOT'])."/file.php");
Chacha102
+1  A: 

Hi brad, in PHP's manual, File System section you find a lot of good examples to do that. Check the links:

Nathan
A: 

You should be able to obtain a path to the correct directory via something like:

str_replace('httpdocs', '', realpath($_SERVER['DOCUMENT_ROOT']));

NB: (You'll move likely need to replace 'httpdocs' with the default directory name your web server uses to serve sites from.)

That said, you'll need to ensure that the file is owned by the same user/group as the web server you're running on. (Usually apache/apache.)

middaparka