(PHP) How to obtain the filename (or file path) when a script is executed as a cron job?
I have a PHP script th开发者_StackOverflow社区at executes perfectly when called via the browser. It fails, however, when called as a cron job. The failure is because the script needs to access its own filename, which I do via $thefilepath = $_SERVER['REQUEST_URI']
. Unfortunately, the $_SERVER['REQUEST_URI']
variable isn't populated when the file is called as a cron job. (I've similarly failed with trying $_SERVER['PHP_SELF']
).
In case anyone is interested, the script needs to access its own filename because the filename contains an integer (for example, filename = myfile4.php
) that is used in the calculation of a subset of rows in a database table. More specifically, myfile4.php
takes rows with IDs from, say, 40-49, whereas myfile5.php
takes rows with IDs from 50-59, etc.
So, my question is, given that $_SERVER['REQUEST_URI']
and $_SERVER['PHP_SELF']
fail to provide the filename (or filepath) for use in a cron job, can anyone please suggest an alternative method of accessing a script's own filename within this cron execution context?
Thank you!
Use __FILE__
or __DIR__
or dirname(__FILE__)
http://www.php.net/manual/en/language.constants.predefined.php
$argv[0]
will always contain the name of the script: http://www.php.net/manual/en/reserved.variables.argv.php
精彩评论