I have a directory structure like the following;
script.php
inc/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 programmatically generate the "/mnt/webdev/[project name]/"
?
An absolute path is defined as specifying the location of a file or directory from the root directory(/). In other words,we can say that an absolute path is a complete path from start of actual file system from / directory. Relative path is defined as the path related to the present working directly(pwd).
An absolute path always contains the root element and the complete directory list required to locate the file. For example, /home/sally/statusReport is an absolute path. All of the information needed to locate the file is contained in the path string.
You can determine the absolute path of any file in Windows by right-clicking a file and then clicking Properties. In the file properties first look at the "Location:" which is the path to the file.
This should work
$root = realpath($_SERVER["DOCUMENT_ROOT"]); include "$root/inc/include1.php";
Edit: added imporvement by aussieviking
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With