为什么要求在当前目录之外的一级文件在 php cron 上显示“未找到”


Why does requiring a file up one level, outside the current directory, show "not found" on php cron?

test3.php

<?php
require("../includes/config.php.inc"); 
echo "done";
?>

当我在网络浏览器中启动它时工作正常(它说完成),但是当我使用 /usr/local/bin/php -f /home3/site/public_html/order/functions/test3.php运行与 Cpanel cron 作业相同的文件,我会收到一封电子邮件,说存在错误。

警告:要求(../include/config.php.inc):无法打开流:第 1 行的/home3/site/public_html/order/functions/cron_renewal.php 中没有这样的文件或目录

区别在于当前目录在从浏览器或 cron 运行时不同。请尝试改用dirname(__FILE__)__DIR__。例如,尝试:

<?php
require(__DIR__ . "/../includes/config.php.inc"); 
echo "done";
?>

在我看来,这是最灵活的方式,因为您不必使cron命令变得复杂(您只需运行脚本),并且可以沿硬盘驱动器移动源,而不必对任何路径进行硬编码。


编辑:相同的方法适用于其他平台,例如Python,Ruby等。 ;)这只是一个"良好做法";)

也许是这样?

cd /home3/site/public_html/order/functions/;/usr/local/bin/php -f test3.php

包含是相对于当前工作目录的。(也许这不是正确的目录...您应该cd到文件的父目录,该目录正在从Web工作。