I'm having a problem with $_SERVER['DOCUMENT_ROOT']. On my home machine (using wamp on win7) the following code works perfect, however when I uploaded it to my host it broke saying unable to find the file, it was clearly missing a directory separator. Is this just a difference between linux and windows? Is there a way around this so that I don't have to change my code whenever I upload to a linux server? I can add . DIRECTORY_SEPARATOR to the definition however this is creating an extra separator on windows (atleast with wamp) the includes still work even with the extra / which is also confusing to me. Is this normal behavior?

$_SERVER manual page and user contributions don't really seem to answer this question, and I'm trying to save headaches when porting to different server enviroments.

// Only works locally
define('DOC_ROOT',$_SERVER['DOCUMENT_ROOT']);
require_once(DOC_ROOT . 'application/config/config.php');

// works locally and on host, even tho extra slash locally
define('DOC_ROOT',$_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR);
require_once(DOC_ROOT . 'application/config/config.php');

    Found some solutions, came up with this that seems to work on wamp and my host.

    
    define('DOC_ROOT',(substr($_SERVER['DOCUMENT_ROOT'],-1,strlen($_SERVER['DOCUMENT_ROOT']))=='/') ? $_SERVER['DOCUMENT_ROOT'] : $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR);
    

      Just a quick comment about your replacement:

      define('DOC_ROOT',(substr($_SERVER['DOCUMENT_ROOT'],-1,strlen($_SERVER['DOCUMENT_ROOT']))=='/') ? $_SERVER['DOCUMENT_ROOT'] : $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR);

      Note that the '/' is still hard-coded in there, despite the code's later use of the DIRECTORY_SEPARATOR constant. So, it's trying to be portable yet still failing to do so everywhere.

      Also note that here are some shorter versions that do the same (with the above error/oversight fixed):

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

      or just:

      define('DOC_ROOT',rtrim($_SERVER['DOCUMENT_ROOT'], DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR);

      And yes, I've normally found that most filesystems (and/or the OS) will filter out null directories, so "C:/test/foo.txt" is the same as "C://test/foo.txt" which is the same as "C://///////test/////////foo.txt" which is the same as... etc.

      EDIT: Woops, forgot PHP isn't as smart as I'd like it to be (e.g. negative character offsets don't work).

        Well when I tested it on the Linux/Apache of my host the directory separator constant gives / just as in the $SERVER var however on my home machine the constant was giving \ but the $SERVER var was still using / Hence why I hard coded it, on the two environments I have access too the $_SERVER variable used / on both but the constant gave \ on windows and / on Linux (as I would expect).

        So my DOC_ROOT ended up being C:/wamp/www/\ instead of C:/wamp/www/ as would be expected with your trim method. Of course maybe this doesn't matter, as like you said i could essentially have 500 slashes for every directory separation and still work, but I try to minimize the chance for error.

          Derokorian wrote:

          So my DOC_ROOT ended up being C:/wamp/www/\ instead of C:/wamp/www/ as would be expected with your trim method.

          Not really; the '/' is not the directory separator on Windows, so it's not PHP's fault that you gave it bad information (e.g. used '/' as the directory separator when defining the document root).

            How did I give it bad information? When did I define the document root? I get the same thing for $_SERVER['DOCUMENT_ROOT'] on WAMP / XP at work.

              Derokorian;10983785 wrote:

              How did I give it bad information?

              "C:/" is actually referring to "C:\" since Windows' filesystem uses "\" as the directory separator. It just so happens, however, that Windows (and/or the filesystem itself) is smart enough to translate an invalid path that uses forward slashes into a valid one that uses backslashes before trying to parse out the different directories.

              Derokorian;10983785 wrote:

              When did I define the document root? I get the same thing for $_SERVER['DOCUMENT_ROOT'] on WAMP / XP at work.

              Ah, well see there's your problem. You didn't define the document root - you let some 3rd party package define it for you, hence why I have never used nor ever recommended using WAMP or any of the other pre-packaged solutions.

                define('DOC_ROOT',
                (substr($_SERVER['DOCUMENT_ROOT'],-1,strlen($_SERVER['DOCUMENT_ROOT']))=='/') ||
                (substr($_SERVER['DOCUMENT_ROOT'],-1,strlen($_SERVER['DOCUMENT_ROOT']))=='\\')
                 ? $_SERVER['DOCUMENT_ROOT'] : $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR);
                

                Problem solved 😛

                  Write a Reply...