Get the last modified date of a remote file Get the last modified date of a remote file curl curl

Get the last modified date of a remote file


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 headerscurl_setopt($curl, CURLOPT_NOBODY, true);//stop it from outputting stuff to stdoutcurl_setopt($curl, CURLOPT_RETURNTRANSFER, true);// attempt to retrieve the modification datecurl_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} 


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}


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?