<?php
/**
* The DocumentRoot class is a simple class that fixes a problem with Windows where PHP does not
* have $_SERVER['DOCUMENT_ROOT'] built in. A single function within this class produces the proper
* Document Root for Windows. The structure on how to call this class is below.
*
* include('includes/classes/DocumentRoot.php');
* $docroot = new DocumentRoot();
* echo $docroot->getDocRoot().'<br><br>';
*
* Additional documentation for the getDocRoot function is included.
*
* @author Allan Bogh - hide@address.com
* @version 1.0 - based on research on www.helicron.net/php
**/
class DocumentRoot{
function DocumentRoot(){
}
/**
* getDocRoot fixes a problem with Windows where PHP does not have $_SERVER['DOCUMENT_ROOT']
* built in. getDocRoot returns what $_SERVER['DOCUMENT_ROOT'] should have. It should work on
* other builds, such as Unix, but is best used with Windows. There are two return cases for
* Windows, one is the document root for the server's web files (c:/inetpub/wwwroot), the
* other version is the first folder beyond that point (if documents are stored in user folders).
*
* @author Allan Bogh - hide@address.com
* @version 1.0 - based on research on www.helicron.net/php
*
* @param $folderFix - This optional parameter tells the function to include the first folder in
* the return (c:/inetpub/wwwroot/userfolder instead of c:/inetpub/wwwroot).
* Set to true if folder should be returned.
* @return The document root string.
**/
function getDocRoot($folderFix=false){
//sets up the localpath
$localpath = getenv("SCRIPT_NAME");
//checks to include the user folder
if($folderFix){
$localpath = substr($localpath,strpos($localpath,'/',1),strlen($localpath));
}
//realpath sometimes doesn't work, but gets the full path of the file
$absolutepath = realpath($localpath);
if((!isset($absolutepath) || $absolutepath=="") && isset($_SERVER['ORIG_PATH_TRANSLATED'])){
$absolutepath = $_SERVER['ORIG_PATH_TRANSLATED'];
}
//checks if Windows is being used to replace the \ to /
if(isset($_SERVER['OS']) && (strpos($_SERVER['OS'],'Windows') > -1)){
$absolutepath = str_replace("\\","/",$absolutepath);
}
//prepares the document root string
$docroot = substr($absolutepath,0,strpos($absolutepath,$localpath));
return $docroot;
}
}
?>