php 获取远程文件的最后修改日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/845220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:03:54  来源:igfitidea点击:

Get the last modified date of a remote file

phpjquerycurl

提问by Mark Henry

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

我想通过 curl 获取远程文件的最后修改日期。有谁知道这是怎么做到的吗?

采纳答案by Daniel Sorichetti

From php's article:

来自php 的文章

<?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 remotefile, since the server should send it to you... Maybe in the HTTP headers?

filemtime() 是这里的关键。但是我不确定您是否可以获得远程文件的最后修改日期,因为服务器应该将其发送给您...也许在 HTTP 标头中?

回答by Tom Haigh

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

您可能可以使用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
} 

回答by h4kuna

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

在 PHP 中,您可以使用本机函数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
}

回答by Pons

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

回答by Patrick Glandien

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-Modifiedis the one that you are interested in.

您可以使用 激活接收回复的标题curl_setopt($handle, CURLOPT_HEADER, true)。您还可以打开 CURLOPT_NOBODY 以仅接收标头,然后通过 \r\n 分解结果并解释单个标头。标题Last-Modified是您感兴趣的标题。

回答by trante

By editing h4kuna's answer I created this:

通过编辑 h4kuna 的答案,我创建了这个:

$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>';

回答by michalzuber

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.

不得不解决类似的问题,但对我来说每天下载一次就足够了,所以我只比较了本地(下载的)缓存文件的修改日期。远程文件没有 Last-Modified 标头。

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

回答by dassouki

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

该链接提供了一些有用的网站,您可以使用它们