3

I'm using PHP cURL module to extract timestamp of a remote file via HTTP headers. I've managed to grab modification timestamp by using CURLOPT_FILETIME constant. Of course, I'm doing this in order too see if the remote file has changed without downloading it's contents.

$ch = curl_init($url);  /* create URL handler */
curl_setopt($ch, CURLOPT_NOBODY, TRUE); /* don't retrieve body contents */
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); /* follow redirects */
curl_setopt($ch, CURLOPT_HEADER, FALSE); /* retrieve last modification time */
curl_setopt($ch, CURLOPT_FILETIME, TRUE); /* get timestamp */
$res = curl_exec($ch);
$timestamp = curl_getinfo($ch, CURLINFO_FILETIME);
curl_close($ch);

What is, in your opinion the best way to check if remote file has changed? Should I go with timestamp check only? Or are there some other clever options that didn't came to my mind?!

aL3xa
  • 35,415
  • 18
  • 79
  • 112
  • If you have ftp access you can find out the actual file modification time. – Explosion Pills Sep 26 '11 at 23:05
  • Why do you follow redirects? Comparing file-sizes can help as well to find out about changes. And there might be ETAG header values. – hakre Sep 26 '11 at 23:08
  • I follow redirect because I'm expecting an image file in the end, and user may provide a shortened link. OTOH, `ETAG` might be provided, but not always. And surely I'll check the file size, too. – aL3xa Sep 26 '11 at 23:47

1 Answers1

4

Your approach looks good for finding the Last-Modified time value. (Need to watch out for -1 return for CURLINFO_FILETIME, meaning that no Last-Modified header value was identified.)

You could save the time returned, and in future checks see if it has changed. If it's changed, fetch the new file via Curl.

Another option would be to save ETag and Last-Modified headers, then use a conditional request to get the image again. This would be more complex, but you'd save the additional HEAD request each time. You can see some of the details in this SO question: if-modified-since vs if-none-match.

Community
  • 1
  • 1
BrianC
  • 10,591
  • 2
  • 30
  • 50