2011-11-23 122 views
4

[求助]通过PHP脚本下载文件导致错误/不同的md5校验和 - 为什么?

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

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

这里是我的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); 
?> 
+1

检查代码中的空格并将其删除。主要通过缩进。当执行不直接在页面上写东西的代码时,不要关闭php,这是一个好习惯。 – Vladimir

+1

确保'<?php'之前或'?>'之后没有换行符或空格。在发送文件之后'退出'也是一个好主意,为了安全起见。 –

+0

您可以检查编码,例如一边是UTF-8,另一边是ISO。如果您的php脚本不在相同的编码,它可能会导致一些校验和错误 –

回答

1

我发现了错误:

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

我是检索使用文件的唯一名称的大小,而不是使用完整路径

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

错误是从

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

和php的错误响应se添加到下载的文件的内容中。

因此,我建议谁在调试时查看是否存在此类问题以评论“Content-Type”,以查看php代码中是否存在一些错误,以及何时所有代码似乎都能正常工作,请重新启用“Content-Type “标题。

在代码我的服务器空间之前

readfile($path.$name_file); 

对校验没有影响

感谢弗拉基米尔和火箭的良好实践提示

1

约标志着这个怎么解决?

I found the error: $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) the error was hidden from header("Content-Type: application/vnd.android.package-archive"); and the php error response added to the content of the downloaded file. Thanks to Vladimir and Rocket for good practice tips – Zorb yesterday

+0

当我发现解决方案无法发布所有细节的答案,所以我张贴为评论。感谢提醒! – Zorb

0

我有下载的PDF类似的问题文件,通过一个fread(尽管readfile给出了同样的问题)。我的最终问题是通过关闭应用程序的调试输出来解决的 - 在我的情况下,它是Wordpress。我将WP_DEBUG设置为true,导致了MD5中的差异。

相关问题