Question

I'm having problems with my include files. I don't seem to be able to figure out how to construct my URLs when I use require_once('somefile.php'). If I try to use an include file in more than one place where the directory structures are different, I get an error that the include file cannot be found.

In asp.net, to get my application root path, I can use ~/directory/file.aspx. The tild forward slash always knows that I am referencing from my website root and find the file no matter where the request comes from within my website. It always refers back to the root and looks for the file from there.

QUESTION: How can I get the root path of my site? How can I do this so I can reuse my include files from anywhere within my site? Do I have to use absolute paths in my URLs?

Thank you!

Was it helpful?

Solution

There is $_SERVER['DOCUMENT_ROOT'] that should have the root path to your web server.

Edit: If you look at most major php programs. When using the installer, you usually enter in the full path to the the application folder. The installer will just put that in a config file that is included in the entire application. One option is to use an auto prepend file to set the variable. another option is to just include_once() the config file on every page you need it. Last option I would suggest is to write you application using bootstrapping which is where you funnel all requests through one file (usually with url_rewrite). This allows you to easily set/include config variables in one spot and have them be available throughout all the scripts.

OTHER TIPS

I usually store config.php file in ROOT directory, and in config.php I write:

define('ROOT_DIR', __DIR__);

And then just use ROOT_DIR constant in all other scripts. Using $_SERVER['DOCUMENT_ROOT'] is not very good because:

  • It's not always matching ROOT_DIR
  • This variable is not available in CGI mode (e.x. if you run your scripts by CRON)

You should use the built in magic constants to find files. __FILE__ and __DIR__. If you are on PHP < 5.3 you should use dirname(__FILE__)

E.g.

require_once __DIR__.'/../../include_me.php';

$_SERVER['DOCUMENT_ROOT'] is not always guaranteed to return what you would expect.

It's nice to be able to use the same code at the top of every script and know that your page will load properly, even if you are in a subdirectory. I use this, which relies on you knowing what your root directory is called (typically, 'htdocs' or 'public_html':

defined('SITEROOT') or define('SITEROOT', substr($_SERVER['DOCUMENT_ROOT'], 0, strrpos($_SERVER['DOCUMENT_ROOT'], 'public_html')) . 'public_html');

With SITEROOT defined consistently, you can then access a config file and/or page components without adapting paths on a script-by-script basis e.g. to a config file stored outside your root folder:

require_once SITEROOT . "/../config.php";

Define it in a config file somewhere.

Assuming you're using an MVC style where everything gets routed through a single index.php then

realpath('.');

Will show you the path to the current working directory (i.e where index.php is)

So then you can define this as

define('PROJECT_ROOT', realpath('.'));

If it's not MVC and you need it to work for files in subfolders then you can just hard code it in a config file

define('PROJECT_ROOT', 'C:/wamp/www/mysite');

Then when including something you can do;

include PROJECT_ROOT . '/path/to/include.php';

You could alternativly set the base directory in your .htaccess file

SetEnv BASE_PATH C:/wamp/www/mysite/

Then in PHP you can reference it with $_SERVER['BASE_PATH']

Try this:

$_SERVER['DOCUMENT_ROOT']
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top