【问题标题】:PHP Cron Job does not give correct directory pathPHP Cron Job 没有给出正确的目录路径
【发布时间】:2019-05-11 19:05:09
【问题描述】:

我的 public_html 目录中有一个 PHP 模块(例如 mycronjob.php)

如果我从浏览器执行 mycronjob.php,它会正常工作。

例如命令 getcwd() 给出 /home/myaccount/public_html 并且所有 fopen 命令都在 public_html 中打开文件。

但是,如果我执行与 cron 作业完全相同的模块,则命令 getcwd() 给出 /home/myaccount 并且所有 fopen 命令都在 myaccount 中打开文件而不是 public_html

Cron 作业是: /usr/local/bin/php -e /home/myaccount/public_html/mycronjob.php

有谁知道为什么 cron 作业 getcwd()fopen() 命令不会解析为 public_html ? 我的主人似乎正在努力寻找原因。

一个典型的fopen()命令是:

$myfile='mydata.htm';
if(($myfile = fopen($myfile,'w')) === FALSE){
echo "Failed to open myfile file for writing!" . "<br />\n";
exit(8);
}

【问题讨论】:

  • 脚本是从用户的本地目录执行的,而不是 public_html 目录。如果当前不是公共目录,则可能php.net/manual/en/function.chdir.php 到公共目录...假设您有权限

标签: php cron


【解决方案1】:

这是因为 cron 是从哪里执行的。这很正常,您可以使用 FILE 常量来修复它。

$file = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mydata.htm';

【讨论】:

    猜你喜欢
    • 2012-10-08
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-22
    • 2014-08-29
    相关资源
    最近更新 更多