簡體   English   中英

PHP強制在Internet Explorer中下載文件失敗

[英]PHP Forced file download failing in Internet Explorer

我在一個網站上使用以下代碼來強制通過PHP下載文件:

$ZipData = file_get_contents($zipFilename);
$ZipSize = filesize($zipFilename);
header("Content-type: application/octet-stream");
header("Content-disposition: attachment; filename=".$ZipTitle.".zip");
echo $ZipData;

盡管這在Chrome和Firefox中完美運行,但在Internet Explorer中卻無能為力。 在谷歌搜索時,我發現了一個可能的解決方案,並將代碼更改為以下內容:

$ZipData = file_get_contents($zipFilename);
$ZipSize = filesize($zipFilename);
if (strstr($HTTP_USER_AGENT,"MSIE")){
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-type: application-download");
    header("Content-Length: $ZipSize");
    header("Content-disposition: attachment; filename=".$ZipTitle.".zip");
    header("Content-Transfer-Encoding: binary");
}else{
    header("Content-type: application/octet-stream");
    header("Content-disposition: attachment; filename=".$ZipTitle.".zip");
}
echo $ZipData;

但是仍然沒有運氣。 我不知道為什么它失敗了,也不知道從哪里開始尋找錯誤或問題,這只是我不知道的一些IE錯誤? 我應該從哪里開始尋找解決方案?

注意:$ ZipTitle始終為'TexturePacker_Pack_xxx',其中xxx為遞增數字。 $ ZipFilename是一個現有的zip文件,在將文件發送到瀏覽器后會取消鏈接。

編輯:有關站點和代碼正在http://www.texturepacker.net上運行

您的Content-Length標頭似乎不正確。 PHP filesize 需要一個字符串並返回一個int。

更改它以實際獲取磁盤上文件的大小:

$zipFile = "C:\ZipFile.zip";  
header("Content-Length: " . filesize($ZipFile));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM