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

用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 Python语言实验
  • ¥15 SAP HANA SQL 增加合计行
  • ¥20 用C#语言解决一个英文打字练习器,有偿
  • ¥15 srs-sip外部服务 webrtc支持H265格式
  • ¥15 在使用abaqus软件中,继承到assembly里的surfaces怎么使用python批量调动
  • ¥15 大一C语言期末考试,求帮助🙏🙏
  • ¥15 ch340驱动未分配COM
  • ¥15 Converting circular structure to JSON
  • ¥30 Hyper-v虚拟机相关问题,求解答。
  • ¥15 TSM320F2808PZA芯片 Bootloader