简体   繁体   English

(PHP)当脚本作为cron作业执行时,如何获取文件名(或文件路径)?

[英](PHP) How to obtain the filename (or file path) when a script is executed as a cron job?

I have a PHP script that executes perfectly when called via the browser. 我有一个PHP脚本,当通过浏览器调用时可以完美执行。 It fails, however, when called as a cron job. 但是,当它被称为cron作业时,它将失败。 The failure is because the script needs to access its own filename, which I do via $thefilepath = $_SERVER['REQUEST_URI'] . 失败的原因是脚本需要访问自己的文件名,我可以通过$thefilepath = $_SERVER['REQUEST_URI'] Unfortunately, the $_SERVER['REQUEST_URI'] variable isn't populated when the file is called as a cron job. 不幸的是,将文件作为cron作业调用时,不会填充$_SERVER['REQUEST_URI']变量。 (I've similarly failed with trying $_SERVER['PHP_SELF'] ). (同样,尝试$_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. 如果有人感兴趣,脚本需要访问自己的文件名,因为文件名包含一个整数(例如filename = myfile4.php ),该整数用于计算数据库表中的行子集。 More specifically, myfile4.php takes rows with IDs from, say, 40-49, whereas myfile5.php takes rows with IDs from 50-59, etc. 更具体地说, myfile4.php接受ID为40-49的行,而myfile5.php接受ID为50-59的行, myfile5.php

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? 因此,我的问题是,鉴于$_SERVER['REQUEST_URI']$_SERVER['PHP_SELF']无法提供用于cron作业的文件名(或文件路径),任何人都可以建议使用另一种方法来访问脚本的在这个cron执行上下文中拥有自己的文件名?

Thank you! 谢谢!

使用__FILE____DIR__dirname(__FILE__) __FILE__ dirname(__FILE__) http://www.php.net/manual/zh/language.constants.predefined.php

$argv[0]将始终包含脚本名称: http : //www.php.net/manual/zh/reserved.variables.argv.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM