Cron task will not open file_get_contents

I am running a PHP script that uses file_get_contents to send a list with what's inside this remote file. If I run the script manually, everything works fine, but when I leave it and wait for the cron to start, it will not receive this deleted content ..... Is this possible? I copy a little code here, where I think the problem is this:

$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 {} 
+4
source share
4 answers

cron does not download code from the "folder" in which you are located, so you need to specify the full path

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

+8
source

File path is different when cron executes

Try

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

Or specify the path yourself

+4
source

You need to make sure that the output and errors are redirected to the file, so you can get an idea of ​​what happens to your script when running cron.

The crontab command will look like this:

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


However, looking at your code, I suggest you use the absolute path to open the flyer.html file, so your script works even when launched from another directory containing this file.

0
source

try like this:

 file_get_contents('flyer.html', true); 
0
source

Source: https://habr.com/ru/post/1395927/


All Articles