Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include PHP files that require an absolute path?

Tags:

php

I have a directory structure like the following;

script.php

inc/include1.php
inc/include2.php

objects/object1.php
objects/object2.php

soap/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]/"?

like image 501
Xenph Yan Avatar asked Aug 07 '08 03:08

Xenph Yan


People also ask

How do you specify an absolute path?

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).

Does absolute path include the file?

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.

How do I create an absolute path to a file?

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.


1 Answers

This should work

$root = realpath($_SERVER["DOCUMENT_ROOT"]);  include "$root/inc/include1.php"; 

Edit: added imporvement by aussieviking

like image 186
Peter Coulton Avatar answered Nov 13 '22 11:11

Peter Coulton