Hi guys.

I need help understanding what this returns:

$SERVER['document_root']

Am I correct in thinking that it returns the top level path of the folder where a website it kept?

And if so, lets say I had a folder inside a folder, inside another folder, and my php file was inside the bottommost (child) folder. And my php file made use of:

SERVER['document_root']

... Would this return a path to the top level folder?

If so, this sounds really god dam handy!

Paul.

    Somewhere in your web server configuration, there is a setting that says what file system directory is the root directory for the (sub-)domain in question. So if, for example, "example.com" is pointed to "/var/www/domains/example" on your web server, then that's what $SERVER['DOCUMENT_ROOT'] (case-sensitive!) will point to. If you want to reference a file that is in a sub-directory, you could reference something like this:

    require_once $_SERVER['DOCUMENT_ROOT'].'/sub_dir/file_name.php';
    

    This would, in this example, then be trying to read the following file:

    /var/www/domains/example/sub_dir/file_name.php
    

      Thanks.

      But what I need to know is - can SERVER['document_root'] .... be used to reference the root folder instead of having to use double periods (..) and a literal path, when referencing the root folder which could be several levels above the one you are currently in?

        If you wanted to access files outside of the web root, then you would have to use the ".." method to work your way up the file system path.

        require_once $_SERVER['DOCUMENT_ROOT'].'/../includes/some_file.php';
        

        And then that means you need to be consistent about where you install those include files (or whatever) in your different environments.

          And you may also have to deal with filesystem permissions issues, and the fact that the server software absolutely cannot access file above a certain directory (In Apache this is "ServerRoot") without some serious hackish stuff being implemented (setting ServerRoot too high is an absolute no-no ... as an example, maybe some things outside ServerRoot could be symbolically linked, and there is mod_userdir to allow access to $HOME if I understand what it's supposed to do correctly ...)

            Write a Reply...