dongyao2022 2010-01-12 17:29
浏览 337
已采纳

用PHP下载CURL后,MD5校验和不正确 - file_get_contents工作正常

I have a script where I have to download some files and to make sure that everything worked fine I'm comparing MD5 checksums.

I have found that the checksums are not correct when downloading with CURL. The script below demonstrates this. It downloads the Google logo and compares checksums.

$url = 'http://www.google.com/intl/en_ALL/images/logo.gif';

echo md5_file($url)."
";

$path = 'f1';
file_put_contents($path, file_get_contents($url));
echo md5_file($path)."
";


$path = 'f2';
$out = fopen($path, 'wb');
$ch = curl_init();
curl_setopt($ch, CURLOPT_FILE, $out);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_exec($ch);
curl_close($ch);
echo md5_file($path)."
";

the output is:

e80d1c59a673f560785784fb1ac10959
e80d1c59a673f560785784fb1ac10959
d83892759d58a1281e3f3bc7503159b5

The first two are correct (they match the MD5 checksum when I download the logo using firefox) and the result produced by curl is not OK.

any ideas how to fix that?

thanks for your help

UPDATE:

interestingly the code below works just fine and produces the correct output. The problem really only seems to exist when saving to a file. Unfortunately I have to save directly to a file since the files I'm downloading can get rather large.

$path = 'f3';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
file_put_contents($path, curl_exec ($ch));
echo md5_file($path)."
";
curl_close ($ch);
  • 写回答

2条回答 默认 最新

  • dsgsgs30201 2010-01-12 17:45
    关注

    You're missing an fclose($out), which could account for md5_file seeing an incomplete file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集