Equivalent to $ _SERVER ['DOCUMENT_ROOT'], which will work when the script is called by cron?

I use $_SERVER['DOCUMENT_ROOT']for my included paths, so the files will determine where they work from (i.e. whether they are live or are being set up) and it works fine, except for scripts executed by cron in which I need to hard indicate the path.

Is it possible to use another variable that can work both from cron and from the browser?

+5
source share
4 answers

When running a PHP script via cron, I assume that it runs in the CLI context instead of the web server. If you are executing PHP from the CLI, $ _SERVER ['DOCUMENT_ROOT'] does not populate correctly. You can use the following code to get around this:

if ($_SERVER['DOCUMENT_ROOT'] == "")
   $_SERVER['DOCUMENT_ROOT'] = dirname(__FILE__);
+11
source

Below is the directory where your script is located:

realpath(dirname(__FILE__));

This works for both web requests and cron scripts.

+5
source

, , , . , - MyAppDirectory/public_html/index.php:

define('APPLICATION_PATH', realpath(dirname(__FILE__).'/..'));

MyAppDirectory/ , index.php . cron, , , . Zend Framework Zend_Application, google "php APPLICATION_PATH" .

+3

chdir(), script cron:

chdir(dirname(__FILE__)); //avoid conflict with "cron path" and app base path (if script runs via 'Cron')

Windows, "nnCron", Linux.

0

All Articles