检查远程文件是否已更改的速度比md5_file()更快


Faster way to check if remote file has changed than md5_file()

在将远程文件保存到服务器之前,我想检查它是否已更改。首先,在使用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));
}

也许有人可以帮我一点忙,或者引导我朝着正确的方向前进^^

问候,Kazuto

我使用了Pack and Unpack函数来做类似的事情。正在将图像和pdf文件转换为二进制文件,而不是md5。我不确定与MD5相比的性能,但看看它很好。

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

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