views:

93

answers:

6

Hi, 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!

+1  A: 

there is $_SERVER['DOCUMENT_ROOT'] that should have the root path to your webserver.

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.

Jonathan Kuhn
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']?
Scott W.
is mysite a virtual host?
koen
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 = ...
dimitris mistriotis
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?
Scott W.
@Scott nope. DOCUMENT_ROOT will always give a file system path` ` ` `
Pekka
OK, I finally got it. Thank you all!
Scott W.
@Scott W. I updated my answer with a few more options and explanations. Good luck.
Jonathan Kuhn
@Jonathan, Thank you!
Scott W.
A: 

$_SERVER['DOCUMENT_ROOT']

sushil bharwani
Nope - the OP is looking for the base URL, not the file path
Pekka
Can't use it to load include files.
Scott W.
@Scott As a matter of fact, the only use of this variable is to load include files
Col. Shrapnel
A: 

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
this won't work in subdirs
Col. Shrapnel
Why would it not work in sub-dirs? `require_once __DIR__.'/i/am/a/sub/dir.php`
Petah
Unfortunately none of these solutions seem to work. $_SERVER['DOCUMENT_ROOT'] comes the closest to the solutions but it does not load include files.
Scott W.
@Scott it does.
Col. Shrapnel
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!
Scott W.
A: 

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
A: 

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
A: 

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

define('ROOT_DIR', dirname(__FILE__));

And then just use ROOT_DIR constant in all other scripts. Using $_SERVER['DOCUMENT_ROOT'] is not very good because: 1) it can be changed 2) this variable is not available in CGI mode (e.x. if you run your scripts by CRON)

SeniorDev
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.
Col. Shrapnel