Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 699
Lin Avatar asked Jul 05 '26 01:07

Lin


2 Answers

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';
like image 79
Ja͢ck Avatar answered Jul 06 '26 15:07

Ja͢ck


You can set a ROOT constant that points to the root:

# on read.php
define('ROOT', dirname(dirname(dirname(dirname(dirname(__FILE__))))).DIRECTORY_SEPARATOR;

and then just call:

# anywhere else
include ROOT.'file/file.doc';
like image 40
Shoe Avatar answered Jul 06 '26 13:07

Shoe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!