【问题标题】:Faster way to check if remote file has changed than md5_file()比 md5_file() 检查远程文件是否已更改的更快方法
【发布时间】: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 大。

标签: php md5-file


【解决方案1】:

我已经使用打包和解包功能来做类似的事情。正在将图像和 pdf 文件转换为二进制而不是 md5。我不确定与 MD5 相比的性能,但看看它很好。

http://php.net/manual/en/function.pack.php

http://php.net/manual/en/function.unpack.php

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-13
    • 1970-01-01
    • 2011-02-14
    • 2016-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多