【发布时间】:2019-04-22 02:06:47
【问题描述】:
我在我的虚拟主机的 CPanel 中设置了一个每分钟运行一次的 cron 脚本。 cron 运行正常,但我遇到了与脚本路径相关的问题,我不知道如何正确解决。我的大部分网站都使用 /home/mysite/public_html/ 作为根路径,但 cron 脚本使用 /home/mysite/ 作为根路径,没有 public_html 部分。
这会导致文件包含出现很多问题,因为我不得不在我的类加载器中通过检查默认路径和以 public_html/ 为前缀的替代路径来解决这个问题:
spl_autoload_register(function($class){
$className = str_replace("\\", "/", $class);
$classPath = "{$className}.php";
$altClassPath = "public_html/{$classPath}";
if(file_exists($classPath)) require $classPath;
elseif(file_exists($altClassPath)) require $altClassPath;
else throw new ClassNotFoundException("Fatal Error: Class {$class} either does not exist, or has its include path mis-configured!");
});
这感觉乏味且容易出错,我一点也不喜欢。有没有更好的方法来处理这个问题?我尝试在 cron 脚本上使用 set_include_path ,但它似乎对自动加载器也没有帮助。
【问题讨论】:
-
chdir()到基本路径?
标签: php cron include-path