Incorrect path when using PHP variable $_SERVER['DOCUMENT_ROOT']
Posted by Support, Last modified by Support on 03 November 2008 01:15 PM
When using the PHP variable $_SERVER['DOCUMENT_ROOT'] you get a path of /var/www/html and this is the incorrect path to your web site files on the server.

To correct this issue please use the following code:

$_SERVER['DOCUMENT_ROOT'] = str_replace( '\\', '/', substr($_SERVER['SCRIPT_FILENAME'], 0, 0-strlen($_SERVER['PHP_SELF']) ) );

Or you can use this code as well:

$_SERVER['DOCUMENT_ROOT'] = str_replace( '\\', '/', substr(__FILE__, 0, 0-strlen($_SERVER['PHP_SELF']) ) );

And to get the parent directory:

$_SERVER['DOCUMENT_ROOT'] = dirname(dirname(__FILE__));

This is because our new linux servers have a different method of hosting configuration and the normal $_SERVER['DOCUMENT_ROOT'] does not return the correct physical path. Using the code above will correct the PHP variable to use the correct path based on the php scripts path and allow you to continue to use the PHP variable $_SERVER['DOCUMENT_ROOT'] which will now return the correct phyiscal path to the document root of your web site files.
(659 vote(s))
Helpful
Not helpful

Comments (0)