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

发布于 2024-12-03 14:00:34 字数 610 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

菊凝晚露 2024-12-10 14:00:34

$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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文