要取得遠端檔案的修改日期,您可以使用 notepad at codewalker dot com 提供的良好函式(經由 dma05 at web dot de 和 madsen at lillesvin dot net 改進)。
但現在您可以使用 stream_get_meta_data (PHP>4.3.0) 更輕鬆地達成相同的結果。
然而,如果發生重新導向,可能會產生問題。在這種情況下,伺服器 HTTP 回應不包含 Last-Modified 標頭,但會有一個 Location 標頭指示檔案的位置。以下函式會處理任何重新導向,即使是多次重新導向,以便您找到想要取得其最後修改日期的實際檔案。
hih,
JJS.
<?php
function GetRemoteLastModified( $uri )
{
$unixtime = 0;
$fp = fopen( $uri, "r" );
if( !$fp ) {return;}
$MetaData = stream_get_meta_data( $fp );
foreach( $MetaData['wrapper_data'] as $response )
{
if( substr( strtolower($response), 0, 10 ) == 'location: ' )
{
$newUri = substr( $response, 10 );
fclose( $fp );
return GetRemoteLastModified( $newUri );
}
elseif( substr( strtolower($response), 0, 15 ) == 'last-modified: ' )
{
$unixtime = strtotime( substr($response, 15) );
break;
}
}
fclose( $fp );
return $unixtime;
}
?>