TechRemarker
TechRemarker

Reputation: 2938

What's the PHP $_SERVER variable for the directory above DOCUMENT_ROOT?

I have a server where the public root is located at: /var/www/example.com/html/.

Using $_SERVER['DOCUMENT_ROOT']); I get the following result:

/var/www/example.com/html/

What $_SERVER array key would I use to get the path directly above the public root? That is, the one here:

/var/www/example.com/

This will have to work on multiple environments, such as local and live. Not sure if there's some way of doing ./ document_root sort of thing.

Upvotes: 4

Views: 2719

Answers (2)

hafichuk
hafichuk

Reputation: 10781

dirname() returns the parent directory of a directory and is platform independent.

dirname($_SERVER['DOCUMENT_ROOT']);

Should return:

/var/www/example.com

If you need the trailing slash then you can append DIRECTORY_SEPARATOR:

dirname($_SERVER['DOCUMENT_ROOT']) . DIRECTORY_SEPARATOR;

Upvotes: 9

Ram
Ram

Reputation: 327

../ is the unixey way to say the folder above so, for example, in a terminal these are the same thing: /home/user/ /home/user/heavymark/../

If OTOH you need support for Windows directory naming you will have to do the work of removing the last bit of the path. Normally you might do this by using a regular expression to capture all but the last filepathbit/ but to support Windows naming you have to fancy it up to handle both filepathbit/ and filepathbit.

Upvotes: 0

Related Questions