【发布时间】:2015-04-04 09:18:08
【问题描述】:
我想在将远程文件保存到我的服务器之前检查它是否已更改。 首先,我在使用远程文件和本地文件之间的 md5_file 执行比较之前检查了文件是否存在,但这真的很慢。有没有更快的方法来检查这个?
$channelName = htmlspecialchars($_GET['channel'], ENT_QUOTES);
$json_array = json_decode(file_get_contents('http://api.hitbox.tv/media/live/'.strtolower($channelName)), true);
$getImage = $json_array['livestream'][0]['channel']['user_logo_small'];
$imageURL = "http://edge.vie.hitbox.tv/$getImage";
$imagePath = str_replace("/static/img/channel/", "", $getImage);
$ImageExtension = substr( strrchr($imagePath, '.'), 1);
$imageOutput = "images/$channelName.".$ImageExtension;
if (file_exists($imageOutput)) {
if (md5_file($imageURL) != md5_file($imageOutput)) {
file_put_contents($imageOutput, file_get_contents($imageURL));
}
} else {
file_put_contents($imageOutput, file_get_contents($imageURL));
}
也许有人可以帮助我或引导我走向正确的方向^^
亲切的问候,和人
【问题讨论】:
-
首先,您可以检查两个文件的大小是否不同。如果他们这样做,你知道 md5-ing 他们会给出不同的结果,并且文件已经改变。另外,我相信逐字节比较文件比 md5 更快。
-
这是http中已解决的问题-使用远程服务器先前返回的标头中的信息(last-modified,etag)创建条件请求并询问是否已更改(远程服务器将如果未更改,则以 304 响应)
-
谢谢@Suppen。我没有考虑比较文件大小。比较它们就足够了,因为它们只有大约 3kb 大。