cron作业将无法打开file_get_contents

问题描述:

我正在运行一个使用file_get_contents的php脚本,以便通过它在远程文件中的内容发送列表。 如果我手动运行脚本一切正常,但是当我离开它,并等待cron运行它不会得到远程内容.....这可能吗? 我在这里复制位代码的地方,我认为这个问题是:cron作业将无法打开file_get_contents

$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 = '[email protected]'; // line to be changed 
$mail->Password = 'xxxx90'; // line to be changed 
$mail->FromName = 'Bob'; // line to be changed 
$mail->From = '[email protected]';// 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 {} 
+0

哪里'flyer.html'居住?如果它是本地的,你应该使用cron的绝对路径。 – 2012-02-11 15:24:55

cron不会从你所在的'文件夹'加载代码,所以你需要指定一个完整的pa日

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

+0

我会尽力,谢谢你,传单是本地的,所以我会改变路径。 – Sebastian 2012-02-11 15:25:37

+1

@Sebastian我喜欢把'chdir(dirname(__ FILE __));'放在cron运行的所有脚本的顶部以防止这个问题。 – DaveRandom 2012-02-11 15:27:16

+0

好主意戴夫! – Richard 2012-02-11 15:28:32

文件的路径是不同的,当cron的执行

尝试

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

或指定你自己的路径

你应该确保输出和错误重定向到一个文件,这样你就可以当cron运行发生了什么事情与你的脚本的想法。

目前的crontab命令是这样的:

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


看你的代码,不过,我会建议你使用绝对路径打开flyer.html文件,所以您的脚本工程,即使从另一个包含该文件的目录启动。

尝试这样的:

file_get_contents('flyer.html', true);