Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

variables

php

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.

like image 516
cchiera Avatar asked Jan 19 '23 05:01

cchiera


1 Answers

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;
like image 123
hafichuk Avatar answered Jan 20 '23 18:01

hafichuk