$_SERVER['DOCUMENT_ROOT'] does not work in the php script running through cron

you could populate the $_SERVER['DOCUMENT_ROOT'] on your own

$_SERVER['DOCUMENT_ROOT'] = dirname(__FILE__);

if the cron file is in document root

$_SERVER['DOCUMENT_ROOT'] = dirname(dirname(__FILE__));

if the cron file is one directory above the document root


Assuming you are running the script directly through cron (as opposed to from a web server accessed by an HTTP request triggered by a cronjob (e.g. by cron running wget)), then of course it doesn't work.

There is no server, so $_SERVER is not set.


$_SERVER cannot be expected to contain any of the normal values when a PHP script is run using the CLI interpreter. Either put the path in an environment variable, or pass it to the script as a command line argument.

Tags:

Php

Cron