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.
hakre source share