How to include PHP files that require an absolute path?
I have a directory structure like the following;
script.phpinc/include1.php
inc/include2.phpobjects/object1.php
objects/object2.phpsoap/soap.php
Now, I use those objects in both
script.php
and /soap/soap.php
, I could move them, but I want the directory structure like that for a specific reason. When executing script.php
the include path is inc/include.php
and when executing /soap/soap.php
it's ../inc
, absolute paths work, /mnt/webdev/[project name]/inc/include1.php...
But it's an ugly solution if I ever want to move the directory to a different location.
So is there a way to use relative paths, or a way to programically generate the
"/mnt/webdev/[project name]/"
?Answer :
This should work
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
include "$root/inc/include1.php";
Edit: added imporvement by aussieviking
source :http://stackoverflow.com/questions/4369/how-to-include-php-files-that-require-an-absolute-path
COMMENTS