Where to install PHP $ _SERVER ['DOCUMENT_ROOT'] Trailing slash?

Sometimes $_SERVER['DOCUMENT_ROOT'] returned with a trailing slash. In other environments, this is not the case. Where can this be indicated?

+6
source share
7 answers

You cannot tell in advance if $_SERVER['DOCUMENT_ROOT'] contains a slash at the end or not.

Usually, if configured correctly, it does not contain a trailing slash. On Ubuntu (as on other UNIX), a correctly written directory path does not have / at the end. For example, on Windows, apache will even refuse to run if it is configured on one. On UNIX, Apache is not legible and allows a trailing slash.

But there is one exception if you create your root directory ( / ) your document root. Because of this, you cannot tell in advance whether it contains a trailing slash.

In any case, it contains the value of the DocumentRoot directive - with or without a trailing slash, as recorded in the httpd configuration file. PHP uses only the apache value. To get the real root of the document, use realpath and / or conditionally add a slash (or remove it) at the end, if either in your configuration file or in your PHP code.

+10
source

I use the current directory more than I use docroot, because it also works well on the command line and in unit tests. I usually use something like:

 require_once(dirname(__FILE__).'/../../../../constants.php'); 

Instead

 require_once($_SERVER['DOCUMENT_ROOT'].'/../constants.php'); 

I saw this first in a Wordpress source and really liked it, but it can lead to a lot of repetitions of "../".

PS FILE is the current file, and dirname will disable /something.php from the end, leaving the path to the directory containing the current file.

+4
source

You can make it so that the trailing slash is always present.

'/'. trim ($ _SERVER ['DOCUMENT_ROOT'], '/'). '/'

 require_once( '/'.trim( $_SERVER['DOCUMENT_ROOT'], '/' ).'/'.'constants.php' ); 
+2
source

I think it depends on the server configuration if the web root is defined with or without a slash. Just check that it is on every system.

See also the ServerRoot apache documentation directive .

+1
source

Hakra's answer is correct. I tried to use include in different situations, in a console console or on a web server. It was best to use an absolute path starting with DOCUMENT_ROOT. But I'm still stuck because of this backside. Here is what seems like a good solution:

include_once getenv ("DOCUMENT_ROOT"). " ./ WEB-INF / classes

Then call the script with php test.php from the script package and put the value in the env variable DOCUMENT_ROOT or not, with a trailing slash or not. When downloading from apache, getenv is already full.

The php processor can work with. /./, telling him the same thing as. /. Just as htdocs./ is interpreted, it has htdocs / (under windows)

0
source

An old question, I know, but since it gave me an idea of ​​how to solve this for myself, I will just add my solution here. I would like to define a constant (ROOTPATH) using $ _SERVER ['DOCUMENT_ROOT'] and make sure that it has a trailing slash (DIRECTORY_SEPARATOR).

 define('ROOTPATH', (ctype_alnum(substr($_SERVER['DOCUMENT_ROOT'], -1)) ? $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR : $_SERVER['DOCUMENT_ROOT'])); 

It is assumed that the last char of the folder name is alphanumeric (I could not recall many folder names that end with special characters). If you want to make sure there is no slash, you can go something like this:

 define('ROOTPATH', (!ctype_alnum(substr($_SERVER['DOCUMENT_ROOT'], -1)) ? substr($_SERVER['DOCUMENT_ROOT'], 0, -1) : $_SERVER['DOCUMENT_ROOT'])); 

Peace, Mo

0
source

When using virtual hosts, Apache writes in $_SERVER['DOCUMENT_ROOT'] the DocumentRoot value of the virtual host. So you can write a trailing braid.

But this is not a good solution, because different hosters interpret this paragraph differently, so your application should not rely on it, and it should determine the presence of a trace without any problems.

0
source

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


All Articles