tags:

views:

639

answers:

4

I use $_SERVER['DOCUMENT_ROOT']."/lib/sft_required.php"; to include the 'sft_required' file in a PHP script. When I run this file using browser, it works fine but when I run this as a cron job job, it does not work. It seems that the file is not included when we run the script through cron.

+4  A: 

$_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.

Ignacio Vazquez-Abrams
+3  A: 

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.

David Dorward
+1  A: 

Reading the documentation should help. Using PHP from the command line

Alex
+2  A: 

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

solomongaby