PHP: determine the directory path of the server root

Contributor Icon Contributed by qmchenry  
Tag Icon Tagged: PHP programming  

It is sometimes necessary to refer to files in the host filesystem explicitly. By retrieving this information dynamically instead of hard coding it, moving the code from one server or hosting company to another is much simpler.


The fully qualified path of the directory containing the root of the web documents is:

$_SERVER["SITE_HTMLROOT"]

To assign the fully qualified path of images/sadie.gif to the variable $pic:

$var = $_SERVER["SITE_HTMLROOT"].'/images/sadie.gif';

 

8 Comments -


  1. Prathiban said on October 9, 2008

    I guess It is
    $_SERVER['DOCUMENT_ROOT'];

  2. George said on December 4, 2008

    this does not work in my php 5 installation.

  3. Dave said on August 11, 2009

    This doesnt work if you running your site from an alias or virtual directory

  4. Anonymous said on September 23, 2009

    Doesn’t work for me. I wonder if it is a super global and these are turned off.

  5. e said on November 4, 2009

    echo(realpath(__FILE__));

  6. Anonymous said on April 29, 2010

    It’s Working Thanx Dude for sharing

  7. Jb said on January 26, 2011

    definitely appreciated

  8. Final said on April 25, 2011

    Thx man :)

 

RSS feed for comments on this post. TrackBack URL

Leave a comment -