Sometimes $_SERVER['DOCUMENT_ROOT'] returns with a trailing slash. In other environments it does not. Where can this be specified?
Sometimes $_SERVER[‘DOCUMENT_ROOT’] returns with a trailing slash. In other environments it does not. Where
Share
You can not say in advance if
$_SERVER['DOCUMENT_ROOT']contains a slash at the end or not.Normally, if properly configured, it does not contain a trailing slash. On Ubuntu (as well as on other UNIX), a properly written path to a directory does not have the
/at the end. On windows for example, apache will even refuse to start if it’s configured with one. On UNIX Apache is not that picky and allows with a trailing slash.But there is one exception, if you make your root directory (
/) your document root. Because of that case, you can not say in advance whether or not it contains a trailing slash.In any case it contains the value of the
DocumentRootdirective – with or without trailing slash like it has been written into the httpd configuration file. PHP only takes over the value from apache. To get the real document root, userealpathand/or conditionally add a slash (or remove it) at the end if either in your configuration file or within your PHP code.