- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个脚本,我必须在其中下载一些文件并确保一切正常,我正在比较 MD5 校验和。
我发现使用 CURL 下载时校验和不正确。下面的脚本演示了这一点。它会下载 Google Logo 并比较校验和。
$url = 'http://www.google.com/intl/en_ALL/images/logo.gif';
echo md5_file($url)."\n";
$path = 'f1';
file_put_contents($path, file_get_contents($url));
echo md5_file($path)."\n";
$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)."\n";
输出是:
e80d1c59a673f560785784fb1ac10959
e80d1c59a673f560785784fb1ac10959
d83892759d58a1281e3f3bc7503159b5
前两个是正确的(当我使用 firefox 下载 Logo 时,它们与 MD5 校验和匹配)并且 curl 生成的结果不正确。
有什么解决办法吗?
谢谢你的帮助
更新:
有趣的是,下面的代码工作正常并产生正确的输出。这个问题似乎只在保存到文件时才存在。不幸的是,我必须直接保存到一个文件中,因为我正在下载的文件可能会变得相当大。
$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)."\n";
curl_close ($ch);
最佳答案
您缺少 fclose($out)
,这可能会导致 md5_file
看到不完整的文件。
关于php - 在 PHP 中使用 CURL 下载后 MD5 校验和不正确 - file_get_contents 工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2050941/
如何在 PHP 中生成 CRC-8 校验和? 最佳答案 function crcnifull ($dato, $byte) { static $PolyFull=0x8c; for ($i=0
我正在编写代码来使用 32 位无符号整数计算 CRC16。当尝试打印执行 CRC 操作的 XOR 函数的返回值时,它总是打印 0。我尝试了各种调试方法,例如打印语句,但是,我似乎无法弄清楚! 这是我的
ThinkPHP3.2.3验证码显示、刷新、校验 ,具体如下: 显示验证码 首先在Home/Controller下创建一个公共控制器PublicController
我想将自定义验证绑定(bind)到 TimePicker 自定义控件,但下面的代码显示“无法将内容添加到 TimePicker 的对象类型。”。
目录 Spring 校验(validator,JSR-303)实现 什么是JSR-303规范 与Spring MVC结合 实体类添加
导包和配置 导入 JSR 303 的包、hibernate valid 的包 ?
我是一名优秀的程序员,十分优秀!