2011-11-23 38 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中的差異。