php 脚本 require() 语句在 运行 时从浏览器获取文件,但在 运行 时从 Plesk CRON 作业获取文件

php script require() statement fetches file when running from browser but not when running from Plesk CRON job

我有一个任务脚本,如果我 运行 通过从我的浏览器访问 php 文件,它可以完美地工作。但是,当我尝试通过 Plesk Task Scheduler 运行 它失败并出现无法加载 require() 文件的致命错误。

require 语句是一个简单的相对路径:

require('../../../app.config.php');

错误是:

PHP Fatal error: require(): Failed opening required '../../../app.config.php'

我认为这可能与 include_path 有关,但我对此了解不多,所以对那个有点迷茫。

任何帮助都会很棒!

您需要使用绝对路径。如果直接调用脚本或脚本包含在另一个脚本中,可能会产生不同的相对路径。

例如:

a.php is on path A/B/C the ../../ will result in in A/
b.php is on path A/B/C/D, if it inlcudes the a.php then the ../../ on a.php will result in A/B/.

当前目录解析的相对路径。解决方案:获取脚本目录并结合相对路径,例如:

 require(dirname(__FILE__).'/../../../app.config.php');