开发者

Curl download image only if not older than 2 days

I want to download an image from a remote server only if it is not older than two days.

Is the code I'm running bellow correct? I want to know the last_modified data before downloading.

  $ch = curl_init($file_source);  // the file we are downloading
  curl_setopt($ch, CURLOPT_TIMEOUT, 20);
  curl_setopt($ch, CURLOPT_FILE, $wh);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($ch, CURLOPT_FILETIME, true);
  curl_exec($ch);
  $headers = curl_getinfo($ch);
  $last_modified = $headers['filetime'];

  if ($last_modified != -1) { // unknown
      echo date("Y-m-d", $last_modified); //etc
  }开发者_运维问答


  curl_close($ch);
  fclose($wh);


$file_source = 'http://www.google.com/images/nav_logo29.png';
$ch = curl_init($file_source);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

curl_setopt($ch, CURLOPT_FILETIME, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); <-- don't download first
curl_exec($ch);
$headers = curl_getinfo($ch);
$last_modified = $headers['filetime'];

if ($last_modified != -1)
{
  if ($last_modified>time()-86400*2) <-- not older than 2 days
  {
    $ch2 = curl_init($file_source);
    $wh  = fopen('YOUR_PATH, 'w');
    curl_setopt($ch2, CURLOPT_FILE, $wh);
    curl_exec($ch2);
    curl_close($ch2);
    fclose($wh);
  }
}
curl_close($ch);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜