Find the PHP Document Root

The document root tells you where your PHP script runs from. Many (most?) pages scripted using PHP seem to assume that the script is running under Apache. It is perfectly possible to run PHP with IIS on a Windows platform. Unfortunately, the environment variables available from IIS are different to thos available from Apache. Searching for tips and code fragments on the net will find you plenty of example but many of them simply assume the platform.

One irritating problem that cropped up when writing the code that generates the original version of these pages was that Apache provides an environment variable called DOCUMENT_ROOT while IIS does not. This variable tells the script where, in the host filing system, the web pages are delivered from and may have a value something like this:

 /usr/local/apache/share/htdocs

If your PHP script needs to find a particular file - a config file for example -  you need to know the document root on the server. Absolute paths may be no good as the scripts can be moved from host to host. Relative paths are awkward because scripts have to be in a fixed location relative to the required file.

If we know where the root of the web is, a global configuration file can live in some fixed location relative to that. Running under Apache, we just add the value of getenv("DOCUMENT_ROOT") to the beginning of the path and we are done. For example

 $docRoot = getenv("DOCUMENT_ROOT");
 include $docRoot."/includes/config.php";

Running under IIS, we have no such convenient shortcut. Instead, we need to work out where the document root is from other information. Both IIS and Apache tell us the name and location of the currently executing script. That information is in an environment variable called SCRIPT_NAME. So we could write

print getenv("SCRIPT_NAME");

and get a result like

/product/description/index.php

This is the full path of the script on the web server - relative to the web root. PHP lets us find the absolute path of this file - relative to the entire server root - with a call to the realpath() function. We only want the name of the file for SCRIPT_NAME, not its full path so we add a call to the basename() function to extract that. Now we can write

print realpath(basename(getenv("SCRIPT_NAME")));

and get something like

/usr/local/apache/share/htdocs/product/description/index.php

Now we have the entire path to the current script and can remove the part that refers to the site-relative file, leaving the document root behind. Make sure that paths for Windows servers have their slashes pointing the same way.

 $localpath=getenv("SCRIPT_NAME");
 $absolutepath=realpath($localPath);
 // a fix for Windows slashes
 $absolutepath=str_replace("\\","/",$absolutepath);
 $docroot=substr($absolutepath,0,strpos($absolutepath,$localpath));
 // as an example of use
 include($docroot."/includes/config.php");

This little code fragment would have to go at the start of any script that needed to know where the document root is but now, your code will run equally with both IIS and Apache servers. Windows 2000 and above, by the way, are quite happy to accept forward slashes in an absolute document path.

See the rest of the PHP archive.

If you want to learn more PHP, you may want to check out some of the books on Amazon:

Comments are closed.