cron job不会打开file_get_contents

Seb*_*ian 5 php cron file-get-contents

我正在运行一个使用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 {}
Run Code Online (Sandbox Code Playgroud)

Ric*_*ard 8

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

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

  • @Sebastian我喜欢把`chdir(dirname(__ FILE __));`放在cron运行的所有脚本的顶部,以防止出现这个问题. (3认同)