6

i'm running a php script that uses file_get_contents in order to mail a list with what it's inside that remote file. If i run the script manually everything works fine, but when i leave it and wait for the cron to run it doesn't get that remote content..... Is that possible? i copy here a bit of the code where i think the problem is:

$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 {}
1
  • Where does flyer.html reside? If it is local, you should use an absolute path for cron. Feb 11, 2012 at 15:24

4 Answers 4

9

The cron doesn't load the code from the 'folder' you are in, so you will need to specify a full path

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

2
  • i'll try that, thanks both, flyer is local, so i'll change the path.
    – Sebastian
    Feb 11, 2012 at 15:25
  • 3
    @Sebastian I like to put chdir(dirname(__FILE__)); at the top of all scripts to be run by cron to prevent this issue.
    – DaveRandom
    Feb 11, 2012 at 15:27
4

The path of the file is different when the cron executes

Try

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

Or specify the path yourself

1

try like this:

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

You should make sure that the output and errors get redirected to a file, so you can get an idea of what's going on with your script when run by cron.

The command in the crontab would look like this :

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


Looking at your code, though, I would suggest you use an absolute path to open the flyer.html file, so your script works even if launched from another directory that the one containing that file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.