How do I find document root?

Hi,

NOTE: wherever you see /localhost/ just imagine there is a “http:/” before it. This is because this forum doesn’t allow you to enter a URL (even though strictly speaking it’s not a URL you can access anyway) until you make 10 forum posts.

I’m building a PHP based website on my local machine and because I have other sites under my localhost I have to put them into folders (e.g. /localhost/SITENAME/).

The problem I’m having is with using absolute paths for my files (e.g. PHP include files, images, css, javascript).

I want to use absolute paths so I can move my PHP web pages into sub folders (e.g. /localhost/SITENAME/PRODUCT-NAME/).

The problem is that I have to manually type into each and every file path “/SITENAME/” at the start, because if I use just a forward slash / then it will try to find the file here: “/localhost/”.

I’ve tried using

$_SERVER['DOCUMENT_ROOT']

but that doesn’t work as expected?

I want to be able to use something like a PHP SERVER variable that can find the root of the website folder and so when I upload the site to the live server I don’t have to change any code because the PHP variable will detect the new server root.

Some of the $_SERVER variables show C:\ rather than /SITENAME/ and others such as $_SERVER[‘SCRIPT_FILENAME’] or $_SERVER[‘PHP_SELF’] show both the sub folder and the current file name (e.g. /localhost/SITENAME/PRODUCT-NAME/index.php) and it seems like it would be a server resource hog to have to call a function which explodes the string into an Array and then just use the relevant part of the URL that I need, but also that it might not work then as expected when putting the site on the live server and so again I have to change the original code which is something I want to avoid.

Any help greatly appreciated as I’m sure there is a fairly simply solution.

Kind regards,

M.

index.php:

$root = dirname( __FILE__ );

#Or for PHP 5.3:
$root = __DIR__;

Hi logic_earth, thanks for posting but this hasn’t worked.

If you take a look at the following example…

echo "<a href='$root'>click here to go to home page</a>";

Produces a link that tries to direct to the path…

file:///C:/Inetpub/wwwroot/SITENAME

…which doesn’t work when you click on the link.

Ideally what I want it to do is to pick up /SITENAME rather than all the c:\ drive information.

Any suggestions? As per my original post I don’t want to have to run a function on every page that explodes the variable into an Array as that code would then break when the site is put on the live server (as it would no longer be in a folder called “SITENAME”).

Thanks.

Kind regards,

M.

Integralist,

You are confusing your terms. Document root is a term that corresponds to the physical location of the files on the server. It doesn’t have anything to do with the URL that your site is accessed at. Both $_SERVER[ ‘DOCUMENT_ROOT’ ] and logic’s method will help you find that.

You are looking for the main folder that this site is accessed at. I can’t think of a way for PHP to know that without changing code in a config file, if you’re going to have the site at /SITENAME on localhost and just at the document root in production.

I always thought this comes close, at least this is what I use:


$filePath = realpath(dirname(__FILE__));
$rootPath = realpath($_SERVER['DOCUMENT_ROOT']);
$htmlPath = str_replace($root, '', $filePath);

echo $htmlPath;

realpath makes sure that the paths exist and that the slashes are the same direction, independent of the OS. Then you just remove the documentroot from the file path…

Am I missing something in this way of thinking?

Yes, it’ll fail for anything using mod_rewrite, apache aliases etc.

Basically, the only to do it that I can think of that will cover all cases is a config file variable or something, or put a <base href=“”> in your <head> section, making all links relative to there.

So, I have been struggling with the same thing. How would I use a config variable? Wouldn’t I have to include the config file in all my other files? Which would defeat the purpose…

As per what Jerrac said I did think to use a include file but it soon became apparent that it wouldn’t work that well at all.

Tomorrow I’ll check out Fristi’s option as I’m not using Apache but a Windows server so the issue that was raised by Stormrider should hopefully not affect my use case. If it works I’ll post my code here in case it helps any one else.

Thanks again for everyone’s post, I’ll let you know tomorrow how far I get or whether it’s back to the drawing board.

Kind regards,

M.

Yes. Why would that defeat the purpose?

Because I would have to include the config file with the full path to get the variable I wanted, when the purpose of that variable is to avoid have to use the full path.

Unless I am missing something obvious…

No, you can include the relative path to the config file:


include dirname(__FILE__) . "/../config.php";

…for example, and in any case, you are getting path mixed up with URL, like the original poster did. We can find the path easily, it’s the URL we want in the config file.

Hi,

Just so you know these solutions didn’t work out for me so it’s back to the drawing board I’m afraid.

If I find out anything more then I’ll let you know.

M.

What i normally do is, just have a configuration file and define the constants with your PATHs/URLs like this:


if($_SERVER['HTTP_HOST'] == "localhost"){// For local
	define('SITE_URL', 'http://' . $_SERVER['HTTP_HOST'] . '/mysitename');
	define('SITEPATH', $_SERVER['DOCUMENT_ROOT'] . '/mysitename');
}
else{ // For Web
	define('SITE_URL', "http://" . $_SERVER['HTTP_HOST']);
	define('SITEPATH', $_SERVER['DOCUMENT_ROOT']);
}

Include the config file at the top of the page and you can use the SITE_URL and SITE_PATH wherever you needed.

1 Like

Maybe I’m just being blind but why can’t you just write the base path to somewhere readily available in your script (a config file, or whatever) and use that value wherever it is needed? Depending on your file structure that might be an easy option.

here is what I do, place an empty file called root.php or whatever extension you want in the root.

then


if (file_exists("./root.php")){
$rpath = '.';
}
elseif (file_exists("../root.php")) {
$rpath = '..';
}
elseif (file_exists("../../root.php")) {
$rpath = '../..';
}
// add more elseif to get your paths

Then in the code you can call images/links like


<a href="<? echo $rpath ?>/link">
<img src="<? echo $rpath ?>/b-img/b-news.gif" width="200" height="30" border="0" class="navbutton">
</a>

This is the only method that is bulletproof for me as the menu can be included at any depth in directories and will always work.

file:///C:/Inetpub/wwwroot/SITENAME

 is a IIS windows server root address. 

Are you using IIS as your dev server or Apache, if you are using IIS this could explain why none of the fixes offered by others here are working. 

The advice most here are offering you is based on the assumption that you are running apache, not IIS

Ummm they would work with IIS just fine. Nothing posted here is server-software-dependent.
PHP on IIS or Apache is not some vastly different language.


If one was following an MVC approach to a web application, or any approach that would funnel every request to a single file (mostly index.php). Then all your problems dealing with paths would come to an end. Second during development building every site or application as it were real is key. By that I mean giving it a real domain and isolated path. (No localhost/site-or-app-name)