<p>I have a directory structure like the following;</p>

<blockquote>
  <p>script.php</p>
  
  <p>inc/include1.php<br/>
  inc/include2.php</p>
  
  <p>objects/object1.php<br/>
  objects/object2.php</p>
  
  <p>soap/soap.php</p>
</blockquote>

<p>Now, I use those objects in both <code>script.php</code> and <code>/soap/soap.php</code>, I could move them, but I want the directory structure like that for a specific reason. When executing <code>script.php</code> the include path is <code>inc/include.php</code> and when executing <code>/soap/soap.php</code> it's <code>../inc</code>, absolute paths work, <code>/mnt/webdev/[project name]/inc/include1.php...</code> But it's an ugly solution if I ever want to move the directory to a different location.</p>

<p>So is there a way to use relative paths, or a way to programmatically generate the <code>"/mnt/webdev/[project name]/"</code>?</p>

[{'answer_id': 4378, 'author': 'Ryan Fox', 'author_id': 55, 'author_profile': 'https://Stackoverflow.com/users/55', 'pm_score': 3, 'selected': False, 'text': '<p>You could define a constant with the path to the root directory of your project, and then put that at the beginning of the path.</p>\n'}, {'answer_id': 4385, 'author': 'Peter Coulton', 'author_id': 117, 'author_profile': 'https://Stackoverflow.com/users/117', 'pm_score': 8, 'selected': True, 'text': '<p>This should work </p>\n\n<pre><code>$root = realpath($_SERVER["DOCUMENT_ROOT"]);\n\ninclude "$root/inc/include1.php";\n</code></pre>\n\n<hr>\n\n<p><strong>Edit:</strong> added imporvement by <a href="https://stackoverflow.com/questions/4369/include-files-requiring-an-absolute-path#4388">aussieviking</a></p>\n'}, {'answer_id': 4388, 'author': 'Christian Hagelid', 'author_id': 202, 'author_profile': 'https://Stackoverflow.com/users/202', 'pm_score': 3, 'selected': False, 'text': '<p>have a look at <a href="http://au.php.net/reserved.variables" rel="nofollow noreferrer">http://au.php.net/reserved.variables</a></p>\n\n<p>I think the variable you are looking for is: <code>$_SERVER["DOCUMENT_ROOT"]</code></p>\n'}, {'answer_id': 4408, 'author': 'Kevin', 'author_id': 40, 'author_profile': 'https://Stackoverflow.com/users/40', 'pm_score': 6, 'selected': False, 'text': '<p>You can use relative paths. Try <code>__FILE__</code>.  This is a PHP constant which always returns the path/filename of the script it is in.  So, in <code>soap.php</code>, you could do:</p>\n\n<pre><code>include dirname(__FILE__).\'/../inc/include.php\';\n</code></pre>\n\n<blockquote>\n  <p>The full path and filename of the\n  file. If used inside an include, the\n  name of the included file is returned.\n  Since PHP 4.0.2, <code>__FILE__</code> always\n  contains an absolute path with\n  symlinks resolved whereas in older\n  versions it contained relative path\n  under some circumstances.\n  <a href="http://us2.php.net/constants.predefined" rel="noreferrer">(source)</a></p>\n</blockquote>\n\n<p>Another solution would be to set an <strong>include path</strong> in your httpd.conf or an .htaccess file.</p>\n'}, {'answer_id': 5187, 'author': 'Polsonby', 'author_id': 137, 'author_profile': 'https://Stackoverflow.com/users/137', 'pm_score': 1, 'selected': False, 'text': '<p>I think the best way is to put your includes in your PHP include path. There are various ways to do this depending on your setup. </p>\n\n<p>Then you can simply refer to </p>\n\n<pre><code>require_once \'inc1.php\';\n</code></pre>\n\n<p>from inside any file regardless of where it is whether in your includes or in your web accessible files, or any level of nested subdirectories.</p>\n\n<p>This allows you to have your include files outside the web server root, which is a best practice.</p>\n\n<p>e.g. </p>\n\n<pre><code>site directory\n    html (web root)\n        your web-accessible files\n    includes\n        your include files\n</code></pre>\n\n<p>Also, check out __autoload for lazy loading of class files</p>\n\n<p><a href="http://www.google.com/search?q=setting+php+include+path" rel="nofollow noreferrer">http://www.google.com/search?q=setting+php+include+path</a></p>\n\n<p><a href="http://www.google.com/search?q=__autoload" rel="nofollow noreferrer">http://www.google.com/search?q=__autoload</a></p>\n'}, {'answer_id': 6463, 'author': 'Polsonby', 'author_id': 137, 'author_profile': 'https://Stackoverflow.com/users/137', 'pm_score': 0, 'selected': False, 'text': "<blockquote>\n  <blockquote>\n    <p>@Flubba, does this allow me to have folders inside my include directory? flat include directories give me nightmares. as the whole objects directory should be in the inc directory.</p>\n  </blockquote>\n</blockquote>\n\n<p>Oh yes, absolutely. So for example, we use a single layer of subfolders, generally:</p>\n\n<pre><code>require_once('library/string.class.php')\n</code></pre>\n\n<p>You need to be careful with relying on the include path too much in really high traffic sites, because php has to hunt through the current directory and then all the directories on the include path in order to see if your file is there and this can slow things up if you're getting hammered. </p>\n\n<p>So for example if you're doing MVC, you'd put the path to your application directoy in the include path and then specify refer to things in the form</p>\n\n<pre><code>'model/user.class'\n'controllers/front.php'\n</code></pre>\n\n<p>or whatever.</p>\n\n<p>But generally speaking, it just lets you work with really short paths in your PHP that will work from anywhere and it's a lot easier to read than all that realpath document root malarkey. </p>\n\n<p>The benefit of those script-based alternatives others have suggested is they work anywhere, even on shared boxes; setting the include path requires a little more thought and effort but as I mentioned lets you start using __autoload which just the coolest.</p>\n"}, {'answer_id': 53079, 'author': 'Darryl Hein', 'author_id': 5441, 'author_profile': 'https://Stackoverflow.com/users/5441', 'pm_score': 2, 'selected': False, 'text': "<p>Another option, related to Kevin's, is use <code>__FILE__</code>, but instead replace the php file name from within it:</p>\n\n<pre><code>&lt;?php\n\n$docRoot = str_replace($_SERVER['SCRIPT_NAME'], '', __FILE__);\nrequire_once($docRoot . '/lib/include.php');\n\n?&gt;\n</code></pre>\n\n<p>I've been using this for a while. The only problem is sometimes you don't have <code>$_SERVER['SCRIPT_NAME']</code>, but sometimes there is another variable similar.</p>\n"}, {'answer_id': 93036, 'author': 'Sam McAfee', 'author_id': 577, 'author_profile': 'https://Stackoverflow.com/users/577', 'pm_score': 3, 'selected': False, 'text': '<p>Another way to handle this that removes any need for includes at all is to use the <a href="http://www.php.net/manual/en/language.oop5.autoload.php" rel="noreferrer">autoload</a> feature. Including everything your script needs "Just in Case" can impede performance. If your includes are all class or interface definitions, and you want to load them only when needed, you can overload the <code>__autoload()</code> function with your own code to find the appropriate class file and load it only when it\'s called. Here is the example from the manual:</p>\n\n<pre><code>function __autoload($class_name) {\n    require_once $class_name . \'.php\';\n}\n\n$obj  = new MyClass1();\n$obj2 = new MyClass2(); \n</code></pre>\n\n<p>As long as you set your include_path variables accordingly, you never need to include a class file again.</p>\n'}, {'answer_id': 9233260, 'author': 'Thane Gill', 'author_id': 1202754, 'author_profile': 'https://Stackoverflow.com/users/1202754', 'pm_score': 2, 'selected': False, 'text': '<p>I found this to work very well!</p>\n\n<pre><code>function findRoot() { \n    return(substr($_SERVER["SCRIPT_FILENAME"], 0, (stripos($_SERVER["SCRIPT_FILENAME"], $_SERVER["SCRIPT_NAME"])+1)));\n}\n</code></pre>\n\n<p>Use:</p>\n\n<pre><code>&lt;?php\n\nfunction findRoot() {\n    return(substr($_SERVER["SCRIPT_FILENAME"], 0, (stripos($_SERVER["SCRIPT_FILENAME"], $_SERVER["SCRIPT_NAME"])+1)));\n}\n\ninclude(findRoot() . \'Post.php\');\n$posts = getPosts(findRoot() . \'posts_content\');\n\ninclude(findRoot() . \'includes/head.php\');\n\nfor ($i=(sizeof($posts)-1); 0 &lt;= $i; $i--) {\n    $posts[$i]-&gt;displayArticle();\n}\n\ninclude(findRoot() . \'includes/footer.php\');\n\n?&gt;\n</code></pre>\n'}, {'answer_id': 29608002, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<pre><code>require(str_repeat(\'../\',(substr_count(getenv(\'SCRIPT_URL\'),\'/\')-1))."/path/to/file.php");\n</code></pre>\n\n<p>I use this line of code. It goes back to the "top" of the site tree, then goes to the file desired.</p>\n\n<p>For example, let\'s say i have this file tree:<br></p>\n\n<blockquote>\n  <p>domain.com/aaa/index.php<br>\n  domain.com/bbb/ccc/ddd/index.php<br>\n  domain.com/_resources/functions.php<br></p>\n</blockquote>\n\n<p>I can include the functions.php file from wherever i am, just by copy pasting</p>\n\n<pre><code>require(str_repeat(\'../\',(substr_count(getenv(\'SCRIPT_URL\'),\'/\')-1))."/_resources/functions.php");\n</code></pre>\n\n<p>If you need to use this code many times, you may create a function that returns the <code>str_repeat(\'../\',(substr_count(getenv(\'SCRIPT_URL\'),\'/\')-1))</code> part. Then just insert this function in the first file you include. I have an "initialize.php" file that i include at the very top of each php page and which contains this function. The next time i have to include files, i in fact just use the function (named <code>path_back</code>):</p>\n\n<pre><code>require(path_back()."/_resources/another_php_file.php");\n</code></pre>\n'}, {'answer_id': 50250763, 'author': 'yuvraj', 'author_id': 9670536, 'author_profile': 'https://Stackoverflow.com/users/9670536', 'pm_score': 0, 'selected': False, 'text': '<p>If you are going to include specific path in most of the files in your application, create a Global variable to your root folder.</p>\n\n<pre><code>define("APPLICATION_PATH", realpath(dirname(__FILE__) . \'/../app\'));\nor \ndefine("APPLICATION_PATH", realpath(DIR(__FILE__) . \'/../app\'));\n</code></pre>\n\n<p>Now this Global variable "<em>APPLICATION_PATH</em>" can be used to include all the files instead of calling realpath() everytime you include a new file.</p>\n\n<p>EX: </p>\n\n<pre><code>include(APPLICATION_PATH ."/config/config.ini";\n</code></pre>\n\n<p>Hope it helps ;-)</p>\n'}]
['https://Stackoverflow.com/questions/4369', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/264/']