2012-02-11 79 views
4

我正在運行一個使用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

回答

7

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

4

文件的路徑是不同的,當cron的執行

嘗試

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

或指定你自己的路徑

0

你應該確保輸出和錯誤重定向到一個文件,這樣你就可以當cron運行發生了什麼事情與你的腳本的想法。

目前的crontab命令是這樣的:

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


看你的代碼,不過,我會建議你使用絕對路徑打開flyer.html文件,所以您的腳本工程,即使從另一個包含該文件的目錄啓動。

0

嘗試這樣的:

file_get_contents('flyer.html', true);