note 73747 added to function.filemtime

From: Date: Thu, 08 Mar 2007 20:02:24 +0000
Subject: note 73747 added to function.filemtime
Groups: php.notes 
Request: Send a blank email to php-notes+get-124243@lists.php.net to get a copy of this message
To get the modification date of some remote file, you can use the fine function by notepad at codewalker dot com (with improvements by dma05 at web dot de and madsen at lillesvin dot net). But you can achieve the same result more easily now with stream_get_meta_data (PHP>4.3.0). However a problem may arise if some redirection occurs. In such a case, the server HTTP response contains no Last-Modified header, but there is a Location header indicating where to find the file. The function below takes care of any redirections, even multiple redirections, so that you reach the real file of which you want the last modification date. hih, JJS. <?php // get remote file last modification date (returns unix timestamp) function GetRemoteLastModified( $uri ) { // default $unixtime = 0; $fp = fopen( $uri, "r" ); if( !$fp ) {return;} $MetaData = stream_get_meta_data( $fp ); foreach( $MetaData['wrapper_data'] as $response ) { // case: redirection if( substr( strtolower($response), 0, 10 ) == 'location: ' ) { $newUri = substr( $response, 10 ); fclose( $fp ); return GetRemoteLastModified( $newUri ); } // case: last-modified elseif( substr( strtolower($response), 0, 15 ) == 'last-modified: ' ) { $unixtime = strtotime( substr($response, 15) ); break; } } fclose( $fp ); return $unixtime; } ?> ---- Server IP: 217.174.196.197 Probable Submitter: 62.212.111.49 ---- Manual Page -- http://www.php.net/manual/en/function.filemtime.php Edit -- https://master.php.net/note/edit/73747 Del: integrated -- https://master.php.net/note/delete/73747/integrated Del: useless -- https://master.php.net/note/delete/73747/useless Del: bad code -- https://master.php.net/note/delete/73747/bad+code Del: spam -- https://master.php.net/note/delete/73747/spam Del: non-english -- https://master.php.net/note/delete/73747/non-english Del: in docs -- https://master.php.net/note/delete/73747/in+docs Del: other reasons-- https://master.php.net/note/delete/73747 Reject -- https://master.php.net/note/reject/73747 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#124243) next »