2012-08-07 93 views
0

這是我的問題。我正在嘗試使用標題下載文件。這裏是我的代碼:PHP強制下載不起作用

$content_type = mime_content_type('uploads/MyBBIntegrator_v1.3.1.zip'); 
$file = 'uploads/MyBBIntegrator_v1.3.1.zip'; 
header("Cache-Control: public"); 
header('Content-type: application/octet-stream'); 
header("Content-Description: File Transfer"); 
header('Content-Disposition: attachment; filename="MyBBIntegrator_v1.3.1.zip'); 
header("Content-Transfer-Encoding: binary"); 
header('Expires: 0'); 
header('Pragma: public'); 
header('Content-Length: ' . filesize($file)); 
readfile('uploads/MyBBIntegrator_v1.3.1.zip'); 

然而,這樣做的唯一結果是,頁面顯示該文件的內容(這是一個文本文件)或奇怪符號的字符串,如果該文件是圖像/ ZIP/exe等

我該怎麼做才能解決這個問題?

+1

刪除內容長度標題並嘗試.. – 2012-08-07 11:29:59

+0

它爲我工作?哪個瀏覽器? – 2012-08-07 13:10:34

+0

作爲開始,刪除所有不需要或未定義的響應頭字段(Content-Description,Content-Transfer-Encoding,Cache-Control:public ...) – 2012-08-13 08:17:36

回答

1

首先,mime_content_type()已棄用,您應該嘗試另一種方法來獲取MIME值。

我檢查了你的代碼,它在我的服務器上工作正常,它對我來說工作正常。您應該檢查可能阻止下載的INI指令。嘗試全新的安裝服務器。

另外,在您將問題放入代碼段之前,腳本不應該生成輸出。