TED
TED

Reputation: 1839

Document root not displaying

I have a script which uses its absolute path to all other included files; the script is going to be execute as a cron job. When I run the script in the terminal the $_SERVER["DOCUMENT_ROOT"] returns a null value, but in the browser it returns the correct document root.

What would cause this problem?

Upvotes: 3

Views: 3162

Answers (5)

stevensagaar
stevensagaar

Reputation: 58

If you are using Magento then instead of using

$_SERVER["DOCUMENT_ROOT"]

or

realpath(dirname(__FILE__))

I would suggest you to use

dirname(Mage::getRoot())

this will give you root folder of your Magento store, hope this helps.

Cheers S

Upvotes: 1

shook
shook

Reputation: 76

I have the need of executing the same scripts in different server environments and have run into the issue of finding the public root of a site especially with command line php. I got sick of editing paths so I wrote a function that finds the root on all of my server environments. I am sure there is a better way but works great for me. some examples of what directory structures it works on:

'/var/www/magento/some/folder/'
'/home/user/html/some/folder/'
'/home/user/public_html/some/folder/'


function getSiteRoot() {
    /*
    ' Returns the root of any of your development/live site as long as the $home array
    ' contains a unique folder name. Define your public directories in this array and the
    ' building function will break when it hits this directory.
    */
    $a = realpath(dirname(__FILE__).'');
    $b = explode('/', $a);
    $home = array("html","public_html","magento"); //add your public folders here
    $str = '';
    foreach ($b as $c) {
        $str.= $c . DIRECTORY_SEPARATOR . '';
            if(in_array($c, $home)) {
                break;
            }
    }
    return $str;
}

Upvotes: 0

Tim
Tim

Reputation: 14446

When php is executed in CLI mode, it behaves differently, including by not setting $_SERVER['DOCUMENT_ROOT']. For a good list of $_SERVER values and other tools available at command line, try executing <?php echo phpinfo();

As a CLI script, you should still have access to __FILE__ to find out what the file you're running is.

Upvotes: 1

Borealid
Borealid

Reputation: 98489

Your issue is that $_SERVER variables are provided by the executing environment. When you run the script on the command line, there is no HTTP server.

So, you can't use things like DOCUMENT_ROOT - what would that be, when there's no Apache configuration setting it?

Instead, you can use variables like __FILE__, which is the full path to the script. Perhaps dirname of that one or more times will get you to the DOCUMENT_ROOT.

Upvotes: 6

Wyzard
Wyzard

Reputation: 34563

I'd expect $_SERVER to be defined only when the script is actually running in a webserver, not when it's run by the standalone command-line PHP interpreter. That value only makes sense in a server context. If the script is meant to run as a cron job, it shouldn't rely on that variable.

Upvotes: 2

Related Questions