29

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!

moinudin
  • 134,091
  • 45
  • 190
  • 216

7 Answers7

25

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.

j08691
  • 204,283
  • 31
  • 260
  • 272
Jonathan Kuhn
  • 15,279
  • 3
  • 32
  • 43
  • On my development machine using WampServer I get C:/wamp/www/ when I use $_SERVER['DOCUMENT_ROOT']. My site however is located in C:/wamp/www/mysite/. If my site was hosted on a live server like a shared host, what would I get when using $_SERVER['DOCUMENT_ROOT']? –  Oct 17 '10 at 09:01
  • 1
    This happens because the actual application root is the C:/wamp/www and you host the code in a subdirectory of this. You might want to change the document root for this project or create a variable (const MY_APP_ROOT = ... – Dimitrios Mistriotis Oct 17 '10 at 09:11
  • 1
    So in an actual hosting environment like a shared host, $_SERVER['DOCUMENT_ROOT'] would yield the path to my site's folder? Am I right? –  Oct 17 '10 at 09:14
  • OK, I finally got it. Thank you all! –  Oct 17 '10 at 09:35
  • @Scott W. I updated my answer with a few more options and explanations. Good luck. – Jonathan Kuhn Oct 18 '10 at 04:08
  • Problem with using `$_SERVER['DOCUMENT_ROOT']` is that it's not available in CLI mode. So if you call your scripts from command-line or by CRON, `$_SERVER['DOCUMENT_ROOT']` will be undefined and you'll get error. That's why using $_SERVER['DOCUMENT_ROOT'] is very bad practice. – barbushin Apr 24 '17 at 09:37
  • @barbushin I wouldn't say that it is "very bad practice" to use the server variable if you never intend on a script to run on the command line. I mean I wouldn't run the wordpress index page over cmd/cron. Also, if you read the edit part I suggest that a config file is used that defines a variable (or a constant) with the path so it is "hard coded" somewhere that isn't expected to change. – Jonathan Kuhn Apr 24 '17 at 19:13
12

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)
S.I.
  • 3,250
  • 12
  • 48
  • 77
barbushin
  • 5,165
  • 5
  • 37
  • 43
  • 5
    to use ROOT_DIR constant in all other scripts, you have to include a file contains it into all other scripts. So, the same problem again. – Your Common Sense Oct 18 '10 at 17:00
  • 1
    Yes, but at least you can just use: include(ROOT_DIR.'/known/path/to/file.php'); Instead of using something like: $ROOT = '../../../'; (which has to change in each sub-directory), but then you have to worry about globalizing $ROOT all the time. – AVProgrammer Jun 30 '12 at 00:26
1

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";
Geoff Kendall
  • 1,307
  • 12
  • 13
0

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.

Petah
  • 45,477
  • 28
  • 157
  • 213
  • Why would it not work in sub-dirs? `require_once __DIR__.'/i/am/a/sub/dir.php` – Petah Oct 17 '10 at 13:22
  • Unfortunately none of these solutions seem to work. $_SERVER['DOCUMENT_ROOT'] comes the closest to the solutions but it does not load include files. –  Oct 17 '10 at 23:50
  • I couldn't for the life of me make it work though. I had to rewrite the whole function and have the includes injected from a different place where the include url is always the same. It was for an authorization script that protects the page its in, based on user name, password and roles. I am brand new to PHP and I love it, but I am going through some growing pains at this stage. I will not do asp.net unless I absolutely have to. PHP has everything I need and so much more. Thanks for all the help! –  Oct 18 '10 at 22:49
  • @anon : Works just fine for me. Perhaps your web server has the document root mis-configured? Mine looks to be pointing to the site's root directory. – winwaed Jun 20 '15 at 00:29
-1

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';
Simon
  • 37,815
  • 2
  • 34
  • 27
-1

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']

Petah
  • 45,477
  • 28
  • 157
  • 213
  • if this paradigm is used, you'd likely run into problems anytime your code was moved to a different directory or a different host. For instance, if you're hosting on a Linux server, then "C:/wamp/www/mysite/" is not going to be where your website lives. I'm not confident this path would even be present on a Windows hosting service. – McAuley Apr 14 '19 at 15:20
-3

Try this:

$_SERVER['DOCUMENT_ROOT']
Rostyslav Dzinko
  • 39,424
  • 5
  • 49
  • 62
sushil bharwani
  • 29,685
  • 30
  • 94
  • 128