36

I would like to get the last modified date of a remote file by means of curl. Does anyone know how to do that?

8 Answers 8

44

You could probably do something like this using curl_getinfo():

<?php
$curl = curl_init('http://www.example.com/filename.txt');

//don't fetch the actual page, you only want headers
curl_setopt($curl, CURLOPT_NOBODY, true);

//stop it from outputting stuff to stdout
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

// attempt to retrieve the modification date
curl_setopt($curl, CURLOPT_FILETIME, true);

$result = curl_exec($curl);

if ($result === false) {
    die (curl_error($curl)); 
}

$timestamp = curl_getinfo($curl, CURLINFO_FILETIME);
if ($timestamp != -1) { //otherwise unknown
    echo date("Y-m-d H:i:s", $timestamp); //etc
} 
1
  • 1
    I've notice that this code sometimes doesn't work, native php get_headers works better for me.
    – Pons
    Nov 21, 2012 at 23:10
26

In PHP you can use the native function get_headers():

<?php
$h = get_headers($url, 1);

$dt = NULL;
if (!($h || strstr($h[0], '200') === FALSE)) {
    $dt = new \DateTime($h['Last-Modified']);//php 5.3
}
3
  • 7
    "If" condition didn't work properly... if(!$h || strpos($h[0], '200') !== false){ works better for me! Jul 1, 2014 at 14:26
  • dino is correct. It looks like a ! operator was omitted in the above code by accident. The if statement should really be if (!(!$h || strstr($h[0], '200') === FALSE)) {
    – Steven
    Apr 3, 2015 at 20:52
  • 2
    Might want to combine and add the lowercase code from Pons to this as well. if(strtolower(trim($k))=='last-modified') Aug 26, 2015 at 18:38
14

From php's article:

<?php
// outputs e.g.  somefile.txt was last modified: December 29 2002 22:16:23.

$filename = 'somefile.txt';
if (file_exists($filename)) {
    echo "$filename was last modified: " . date ("F d Y H:i:s.", filemtime($filename));
}
?>

filemtime() is the key here. But I'm not sure if you can get the last modified date of a remote file, since the server should send it to you... Maybe in the HTTP headers?

4
  • 2
    From the manual: "As of PHP 5.0.0, this function can also be used with some URL wrappers."
    – nickf
    May 10, 2009 at 12:30
  • 4
    For my experience this method doesn't work always (it dependes on your php.ini) so native get_headers worked better for me.
    – Pons
    Nov 21, 2012 at 23:14
  • 1
    filemtime() only works with files on the same server. Use $ch = curl_init("domian.com/dir/file.htm"); curl_setopt($ch, CURLOPT_NOBODY, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FILETIME, true); $result = curl_exec($ch); $retcode= curl_getinfo($ch, CURLINFO_FILETIME); curl_close($ch); Aug 21, 2021 at 15:48
  • Why is this marked as the correct answer? As @MarkAntonyAgius notes, filemtime() won't work for remote files. Mar 11, 2022 at 21:44
3

Sometimes header come with different upper lower case, this should help:

function remoteFileData($f) {
    $h = get_headers($f, 1);
    if (stristr($h[0], '200')) {
        foreach($h as $k=>$v) {
            if(strtolower(trim($k))=="last-modified") return $v;
        }
    }
}
2

You can activate receiving the headers of the reply with curl_setopt($handle, CURLOPT_HEADER, true). You can also turn on CURLOPT_NOBODY to only receive the headers, and after that explode the result by \r\n and interpret the single headers. The header Last-Modified is the one that you are interested in.

3
  • Yep - just request the headers May 10, 2009 at 18:00
  • 1
    Assuming they send the Last-Modified header May 11, 2009 at 3:40
  • This seems to have a problem with 403 - forbidden - responses - it doesn't seem to get access to the header. Don' think there is a way around that.
    – Steve
    May 4, 2021 at 10:34
1

By editing h4kuna's answer I created this:

$fileURL='http://www.yahoo.com';
$headers = get_headers($fileURL, 1);
$date = "Error";
//echo "<pre>"; print_r($headers); echo "</pre>";
if ( $headers && (strpos($headers[0],'200') !== FALSE) ) {
    $time=strtotime($headers['Last-Modified']);
    $date=date("d-m-Y H:i:s", $time);
}
echo 'file: <a href="'.$fileURL.'" target="_blank">'.$fileURL.'</a> (Last-Modified: '.$date.')<br>';
0

would something like this work, from web developer forum

<? $last_modified = filemtime("content.php"); print("Last Updated - ");
print(date("m/d/y", $last_modified)); ?

// OR

$last_modified = filemtime(__FILE__); 

the link provides some useful insite on you can use them

0

Had to solve similiar issue, but for me download once a day was enough so I compared just the modify day of the local (downloaded) cache file. The remote file had no Last-Modified header.

$xml = 'test.xml';
if (is_file($xml) || date('d', filemtime($xml)) != date('d')) {
    $xml = file_get_contents(REMOTE_URL);
}

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.