Take the 2-minute tour ×

I thought I would ask in case I could do it a better way.

On my local (WAMP) I have all my website in the www folder. ( C:\wamp\www )

Now currently I do this when i include a file:


When I am working on local and upload site to a webhost i want to ensure the paths don't breakI

Could someone please tell me if I should be doing it this way?

I want to ensure maximum compatibility; meaning that paths won't break if I, for example, move site from local to whatever web host I decided to use or if I, for example, move from one host to another.

Maybe there is a more bullet proof way of doing it?

share|improve this question
Watch out: DOCUMENT_ROOT may not be defined in all circumstances, such as when running scripts from a command prompt. –  Charles Apr 22 '11 at 16:53
That's already proofed against server changes. It doesn't deal with relocations in a directory below the DOCUMENT_ROOT of course. For that it's common to use application-wide constants for example. –  mario Apr 22 '11 at 16:54

4 Answers 4

up vote 1 down vote accepted

What I usually do, is make 1 config file (which might include others) with a few very basic constants:

define('PROJECT_ROOT', dirname(dirname(__FILE__))); // or dirname(__DIR__) for PHP 5.3
// etc

Al my other files/includes will be based on those very simple constants. I will never need relative paths and never the include_path, because both PROJECT_ROOT and PROJECT_WEB are 'real'/absolute.

Other useful (?) constants would be PROJECT_LOGIC and/or PROJECT_CONTROLLERS and/or PROJECT_3RD_PARTY etc.

share|improve this answer
your answer seems the most efficent and practial call for me. I do something similar on where my config file contains constants for site name, site email, website address and my conig.php also includes() other files like header.inc.php, sidebar.inc.php, functions.inc.php, db.inc.php, paths.inc.php. So essentially i only ever then have to have 2 includes on each new page i create for site which is config.php and footer.inc.php and even the footer is like include($footer_var) Thanks phplover –  PHPLOVER Apr 22 '11 at 18:11

The problem with using $_SERVER['DOCUMENT_ROOT'] is that it will break if you move your PHP scripts up or down a directory level. Instead use this:

require_once(dirname(__FILE__) . "/lib/config.php");

__FILE__ is the absolute path of the script. dirname() removes the last path component (the script filename) so you can append other path components to it, like /lib/config.php or /../../lib/config.php or whatever. This will work everywhere.

PHP 5.3 introduced a shorthand for dirname(__FILE__), called __DIR__, but this doesn't work in <5.3.

share|improve this answer
this wont work if hes including the file from /index.php and from /dir/dir2/index.php –  Galen Apr 22 '11 at 16:53
@Galen, can you explain what you mean? –  Charles Apr 22 '11 at 16:54
getting the directory of a file located at /dir/dir2/ would run require(dir/lib/config.php) –  Galen Apr 22 '11 at 16:56

You should see Include path. For that set_include_path is useful.

share|improve this answer
To have more than 1 include_path is very expensive. Realpaths are better/faster. –  Rudie Apr 22 '11 at 17:28

That works fine for including the config file (although i would get rid of the beginning quotes)

require_once( $_SERVER['DOCUMENT_ROOT'] . "/lib/config.php" );

This is really the only way to do it if you are including the config file from a bunch of different directories.

On bigger project id say that "most" developers have a front controller that all scripts are loaded from. This front controller loads the config file and since its the same file always including the config file there's no need for $_SERVER['DOCUMENT_ROOT'].

share|improve this answer
thanks for the link, very interesting about the front controller, has brought up more questions that i am researching in more depth. Thanks! –  PHPLOVER Apr 22 '11 at 18:18

Your Answer


