Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$_SERVER['DOCUMENT_ROOT'] creates extra slash at end

Tags:

php

When I use $_SERVER['DOCUMENT_ROOT'] in my localhost it outputs: C:/apache2.2/htdocs.

However when I use it on my server it outputs: /var/www/

I can't use stripslashes() since it removes all slashes even from beginning. Any suggestion how to counter this? I don't mind if the outputs has slash or none at end. But I just want it to be the same for both files. So I don't keep changing paths by adding or removing slashes.

Btw I cannot change anything on my server. However I can change my local to match the server but don't know how.

like image 272
aozora Avatar asked Dec 14 '25 18:12

aozora


1 Answers

Try

rtrim($_SERVER['DOCUMENT_ROOT'], '/')

to normalise the string. See http://php.net/manual/function.rtrim.php.

I strongly recommend never relying on DOCUMENT_ROOT as it is an external dependency. Instead, use the magic constants __DIR__ and __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

See http://php.net/manual/language.constants.predefined.php

like image 146
Phil Avatar answered Dec 16 '25 10:12

Phil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!