通过PHP脚本下载文件会导致错误/不同的md5校验和-为什么? [英] Downloading a file via PHP script results in wrong/different md5 checksum - why?

查看:142
本文介绍了通过PHP脚本下载文件会导致错误/不同的md5校验和-为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过PHP实现间接下载.在客户端,我使用md5验证下载的文件是否正确.

I'm trying to implement an indirect download through PHP. On the client side I verify if the downloaded file is correct or not using md5.

当我直接下载文件时( http://server/folder/file.apk )我得到与文件系统相同的md5校验和,但是当我通过PHP脚本下载它时( http://server/some_page.php ),我得到了完全不同的校验和.为什么?

When I download the file directly (http://server/folder/file.apk) I get the same md5 checksum as on the file system, but when I download it via the PHP script (http://server/some_page.php) I get a totally different checksum. Why?

这是我的PHP脚本:

<?php
$name_file="test2.apk";
$path="/home/user/public_html/apk/"; 
$dimension_file=(string)filesize($name_file);

header("Content-Type: application/vnd.android.package-archive ; name=".$name_file);
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$dimension_file);
header("Content-Disposition: inline; filename=".$name_file);
header("Expires: 0");
header("Cache-Control: no-cache, must-revalidate");
header("Cache-Control: private");
header("Pragma: public");

readfile($path.$name_file);
?>

推荐答案

我发现了错误:

 $name_file="test2.apk";
 $path="/home/user/public_html/apk/"; 
 $dimension_file=(string)filesize($name_file); //<-- HERE! --- 

我只使用文件名而不是完整路径来获取大小

i was retrieving the size using only the name of file instead of using the full path

filesize($name_file) ---> filesize( $path . $name_file) 

该错误已从

header("Content-Type: application/vnd.android.package-archive"); 

并将php错误响应添加到下载文件的内容中.

and the php error response added to the content of the downloaded file.

因此,我建议谁在调试时查看"Content-Type"问题,以查看php代码中是否存在某些错误,以及何时所有代码似乎都可以正常工作,然后重新启用"Content-Type"标头.

So i suggest to who has this kind of problems to comment the "Content-Type" while debugging to see if there are some errors in the php code and when all code seems to work re-enable the "Content-Type" header.

在我的服务器空间中

readfile($path.$name_file);

对校验和没有影响

感谢弗拉基米尔(Vladimir)和火箭(Rocket)提供的良好练习提示

Thanks to Vladimir and Rocket for good practice tips

这篇关于通过PHP脚本下载文件会导致错误/不同的md5校验和-为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆