$ _SERVER ['DOCUMENT_ROOT'] creates an extra slash
When I use $_SERVER['DOCUMENT_ROOT']
in my local host, it provides: C:/apache2.2/htdocs
.
However, when I use it on my server, it outputs: /var/www/
I cannot use stripslashes()
as it removes all forward slashes even from the beginning. Any suggestion how to counter this? I don't mind if the outputs have a slash or not at the end. But I just want it to be the same for both files. Therefore, I am not modifying the paths by adding or removing the forward slash.
Btw I can't change anything on my server. However, I can change my local one to match the server, but I don't know how.
source to share
Try
rtrim($_SERVER['DOCUMENT_ROOT'], '/')
to normalize the string. See http://php.net/manual/function.rtrim.php .
I highly recommend never relying on DOCUMENT_ROOT as it is an external dependency. Use magic constants __DIR__
and instead __FILE__
to refer to paths relative to your scripts. For example...
$someDirRelativeToThisFile = __DIR__ . '/some-dir'; // PHP >= 5.3.0
$someDirRelativeToThisFile = dirname(__FILE__) . '/some-dir'; // PHP < 5.3.0
source to share