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
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
}

- 57,217
- 21
- 114
- 142
-
1I've notice that this code sometimes doesn't work, native php get_headers works better for me. – Pons Nov 21 '12 at 23:10
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
}
-
7"If" condition didn't work properly... `if(!$h || strpos($h[0], '200') !== false){` works better for me! – kuzey beytar Jul 01 '14 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 03 '15 at 20:52
-
2Might want to combine and add the lowercase code from Pons to this as well. `if(strtolower(trim($k))=='last-modified')` – Armstrongest Aug 26 '15 at 18:38
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?

- 1,921
- 1
- 20
- 34
-
2From the manual: "As of PHP 5.0.0, this function can also be used with *some* URL wrappers." – nickf May 10 '09 at 12:30
-
4For 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 '12 at 23:14
-
1filemtime() only works with files on the same server. Use $ch = curl_init("https://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); – Mark Antony Agius Aug 21 '21 at 15:48
-
Why is this marked as the correct answer? As @MarkAntonyAgius notes, filemtime() won't work for remote files. – Scott C Wilson Mar 11 '22 at 21:44
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;
}
}
}

- 1,747
- 1
- 13
- 19
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.

- 7,791
- 5
- 41
- 47
-
-
1
-
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 04 '21 at 10:34
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>';

- 33,518
- 47
- 192
- 272
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);
}

- 5,079
- 2
- 28
- 29
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

- 6,286
- 7
- 51
- 81