【问题标题】:cron job won't open file_get_contentscron 作业不会打开 file_get_contents
【发布时间】:2012-02-11 23:29:09
【问题描述】:

我正在运行一个使用 file_get_contents 的 php 脚本,以便通过邮件发送一个列表,其中包含该远程文件中的内容。 如果我手动运行脚本,一切正常,但是当我离开它并等待 cron 运行时,它不会获得远程内容.....这可能吗? 我在这里复制一些我认为问题所在的代码:

$flyer = file_get_contents('flyer.html');

$desti = $firstname." ".$lastname;

$mail = new phpmailer();

$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "orion.xxxx.com"; // line to be changed
$mail->Port = 465; // line to be changed
$mail->Username = 'bob@xxxx.com'; // line to be changed
$mail->Password = 'xxxx90'; // line to be changed
$mail->FromName = 'Bob'; // line to be changed
$mail->From = 'bob@xxxx.com';// line to be changed

$mail->AddAddress($email, $desti);

$mail->Subject = 'The Gift Store';    // to be changed



if ($cover_form == '1'){ $mail->MsgHTML($long10);} else
if ($cover_form == '2'){ $mail->MsgHTML($customer);} else
if ($cover_form == '3'){ $mail->MsgHTML($freedoers);} else
if ($cover_form == '4'){ $mail->MsgHTML($freelongform);}  else
if ($cover_form == '5'){ $mail->MsgHTML($freestoreshort);}  else
if ($cover_form == '6'){ $mail->MsgHTML($getasiteshort);}  else
if ($cover_form == '7'){ $mail->MsgHTML($flyer);}  
else {}

【问题讨论】:

  • flyer.html 位于何处?如果是本地的,你应该为 cron 使用绝对路径。

标签: php cron file-get-contents


【解决方案1】:

cron 不会从您所在的“文件夹”加载代码,因此您需要指定完整路径

$flyer = file_get_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . "flyer.html");

【讨论】:

  • 我会试试的,谢谢两位,传单是本地的,所以我会改变路径。
  • @Sebastian 我喜欢将 chdir(dirname(__FILE__)); 放在所有由 cron 运行的脚本的顶部,以防止出现此问题。
【解决方案2】:

cron执行时文件路径不同

试试

$flyer = file_get_contents(__DIR__ . '/flyer.html');

或者自己指定路径

【讨论】:

    【解决方案3】:

    试试这样:

    file_get_contents('flyer.html', true);
    

    【讨论】:

      【解决方案4】:

      您应该确保将输出和错误重定向到一个文件,这样您就可以了解您的脚本在由 cron 运行时发生了什么。

      crontab 中的命令如下所示:

      php /path/to/your/script.php >/tmp/log.txt 2>&1
      


      不过,查看您的代码,我建议您使用绝对路径打开 flyer.html 文件,因此即使从包含该文件的另一个目录启动您的脚本也能正常工作。

      【讨论】:

        猜你喜欢
        • 2011-09-14
        • 1970-01-01
        • 2011-08-15
        • 2012-07-17
        • 2017-03-27
        • 1970-01-01
        • 2011-05-29
        • 1970-01-01
        • 2014-08-12
        相关资源
        最近更新 更多