简体   繁体   中英

Saving graphic to local folder from cron job

I have a cron job that runs once a week and checks a remote site for any updates, which may include new data, and may include new graphics.

I check my local folder to see if the graphic already exists, and if it doesnt then I want to save a local copy of that graphic.

My code seems to work when I run it from the browser, but when I check the folder every week, there are a lot of empty graphic files.

They have the correct filenames, but are all zero bytes.

My code inside the php file that the cron job runs is this:

if (!file_exists($graphic)) {
  $imageString = file_get_contents($graphicurl);
  file_put_contents($graphic, $imageString);
}

$graphic will be something like "filename.jpg"

$graphicurl will be something like " https://remotegraphicfile.jpg "

And I see many files such as "filename.jpg" that exist in my local folder, but with a zero byte filesize.

Is there any reason why this wouldnt work when called by a cron job?

I have now managed to get this working, by changing my original code from:

if (!file_exists($graphic)) {
  $imageString = file_get_contents($graphicurl);
  file_put_contents($graphic, $imageString);
}

To this:

if (!file_exists($graphic)) {
  $file = fopen ($graphic, 'w');
  $c = curl_init($graphicurl);
  curl_setopt($c, CURLOPT_FILE, $file);
  curl_exec($c);
  curl_close($c);
  fclose($file);
}

This now works, and graphics save to my folder when running the cron job.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM