Science, Tech, Math › Computer Science Finding the PHP Document Root Finding the PHP Document Root on Apache and IIS Servers Share Flipboard Email Print Kohei Hara/Getty Images Computer Science PHP Programming Tutorials MySQL Commands Perl Python Java Programming Javascript Programming Delphi Programming C & C++ Programming Ruby Programming Visual Basic View More By Angela Bradley Angela Bradley Computer Science Expert B.A, History, Eastern Oregon University Angela Bradley is a web designer and programming expert with over 15 years of experience. An expert in iOS software design and development, she specializes in building technical hybrid platforms. Learn about our Editorial Process Updated on February 24, 2019 The PHP document root is the folder where a PHP script is running. When installing a script, web developers often need to know the document root. Although many pages scripted with PHP run on an Apache server, some run under Microsoft IIS on Windows. Apache includes an environment variable called DOCUMENT_ROOT, but IIS doesn't. As a result, there are two methods for locating the PHP document root. Finding the PHP Document Root Under Apache Instead of emailing tech support for the document root and waiting for someone to respond, you can use a simple PHP script with getenv (), which provides a shortcut on Apache servers to the document root. These few lines of code return the document root. Finding the PHP Document Root Under IIS Microsoft's Internet Information Services was introduced with Windows NT 3.5.1 and has been included in most Windows releases since then—including Windows Server 2016 and Windows 10. It does not supply a shortcut to the document root. To find the name of the currently executing script in IIS, begin with this code: print getenv ("SCRIPT_NAME"); which returns a result similar to: /product/description/index.php which is the full path of the script. You don't want the full path, just the name of the file for SCRIPT_NAME. To get it, use: print realpath(basename(getenv("SCRIPT_NAME"))); which returns a result in this format: /usr/local/apache/share/htdocs/product/description/index.php To remove the code referring to the site-relative file and arrive at the document root, use the following code at the beginning of any script that needs to know the document root. $localpath=getenv("SCRIPT_NAME");$absolutepath=realpath($localPath);// fix the Windows slashes$absolutepath=str_replace("\\","/",$absolutepath);$docroot=substr($absolutepath,0,strpos($absolutepath,$localpath));// an example of useinclude($docroot."/includes/config.php"); This method, although more complex, runs on both IIS and Apache servers. Cite this Article Format mla apa chicago Your Citation Bradley, Angela. "Finding the PHP Document Root." ThoughtCo, Aug. 27, 2020, thoughtco.com/finding-the-document-root-2693942. Bradley, Angela. (2020, August 27). Finding the PHP Document Root. Retrieved from https://www.thoughtco.com/finding-the-document-root-2693942 Bradley, Angela. "Finding the PHP Document Root." ThoughtCo. https://www.thoughtco.com/finding-the-document-root-2693942 (accessed March 23, 2023). copy citation Featured Video