What is the PHP variable $ _SERVER for the directory above DOCUMENT_ROOT?

I have a server where the open 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 key would I use to get the path directly above the open root? That is, here:

 /var/www/example.com/ 

This will need to work in several environments, such as local and live. Not sure if there is a way to do ./ document_root.

+4
source share
2 answers

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

 dirname($_SERVER['DOCUMENT_ROOT']); 

Must return:

 /var/www/example.com 

If you need a trailing slash, you can add DIRECTORY_SEPARATOR :

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

../- this is the unixey way of saying the folder above, so, for example, in the terminal it is one and the same: / Home / user / / Home / user / heavymark/../

If you need OTOH support for naming a Windows directory, you will have to do the work of removing the last bit of the path. Typically, you can do this using a regular expression to capture everything except the last pathbitbit / file, but to support the Windows names that you have to come up with to handle filepathbit and filepathbit files.

0
source

Source: https://habr.com/ru/post/1382809/


All Articles