When I try to use require
or require_once
, it will work fine if the file to be required is in the same subdirectory, but the moment it sees a file outside of their subdirectory, it generates a fatal error. Here is basically what the file tree looks like:
* main.php
+ login/
* login.php
+ includes/
* session.php
...so basically, if I were to have main.php require login/login.php, it's fine, but if I try to do directory traversal for login.php to require includes/session.php, it fails, resulting in the error:
PHP Fatal error: require_once(): Failed opening required [...]
The code for this concept would be:
require('login/login.php') #works fine (main.php)
require('../includes/session.php') #quits (login.php)
I have tried using $_SERVER('DOCUMENT_ROOT')
, $_SERVER('SERVER_ADDR')
, dir(_FILE_)
, and chdir(../)
. I remember working on a project a few months back that I solved this problem on, it was a simple function that traversed file paths, but I can't find it anymore. Any ideas?