(PHP) 当脚本作为 cron 作业执行时如何获取文件名(或文件路径)?
我有一个 PHP 脚本,通过浏览器调用时可以完美执行。然而,当作为 cron 作业调用时,它会失败。失败是因为脚本需要访问自己的文件名,我是通过 $thefilepath = $_SERVER['REQUEST_URI'] 来访问的。不幸的是,当文件作为 cron 作业调用时,$_SERVER['REQUEST_URI']
变量不会被填充。 (我尝试 $_SERVER['PHP_SELF']
也同样失败了)。
如果有人感兴趣,脚本需要访问自己的文件名,因为文件名包含一个整数(例如,filename = myfile4.php
),该整数用于计算 a 中的行子集。数据库表。更具体地说,myfile4.php
获取 ID 为 40-49 的行,而 myfile5.php
获取 ID 为 50-59 等的行
。问题是,鉴于 $_SERVER['REQUEST_URI']
和 $_SERVER['PHP_SELF']
无法提供用于以下用途的文件名(或文件路径)一个 cron 作业,任何人都可以建议一种在此 cron 执行上下文中访问脚本自己的文件名的替代方法吗?
谢谢你!
I have a PHP script that 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
__FILE__
或__DIR__
或dirname(__FILE__)
http://www.php.net/manual/en/language.constants .预定义.php
Use
__FILE__
or__DIR__
ordirname(__FILE__)
http://www.php.net/manual/en/language.constants.predefined.php
$argv[0]
将始终包含脚本的名称:http://www.php.net/manual/en/reserved.variables.argv.php$argv[0]
will always contain the name of the script: http://www.php.net/manual/en/reserved.variables.argv.php