Search code examples
phpfiledirectorydirectory-structure

Methods for accessing files outside root


Hi all :D My file structure is as follow:

/site
  /public_html
    /1
      /2
        /3
          read.php
  /file
    file.doc

For security reasons, I moved my docs outside public_html folder. Inside read.php, I am accessing file.doc via code "../../../../file/file.doc" Are there other methods (shortcuts?) to access the doc? I call on a lot of files from many locations. This code is too long and easy to lose track on how many ../ I need.


Solution

  • If your virtual host is rooted on /site/public_html you can shorten it a little by using $_SERVER['DOCUMENT_ROOT']; this should work for all scripts underneath the document root in the same manner.

    $file = $_SERVER['DOCUMENT_ROOT']. '/../file/file.doc';