2012-10-10 61 views
0

我在那里我用下面的代码通过PHP来强制文件下载网站:PHP强制文件下载在Internet Explorer中失败

$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; 

但仍然没有运气。我不知道为什么它失败了,也不知道从哪里开始寻找错误或问题,这只是一些I.E.错误我不知道?我应该从哪里开始尝试寻找解决方案?

注意:$ ZipTitle将始终为'TexturePacker_Pack_xxx',其中xxx是递增的数字。 $ ZipFilename是一个现有的zip文件,该文件在文件发送到浏览器后被解除链接。

编辑:网站和有关的代码都在行动上http://www.texturepacker.net

+0

你是哪里定义的第二个解决方案'ZipSize'? –

+0

什么版本的IE? – PenguinCoder

+0

下面的代码Igniter的帮手,你应该尝试'Content-Type:application/x-zip' – Touki

回答

0

Content-Length头,似乎是不正确的。 PHP filesizeexpects a string并返回一个int。

更改它真正得到磁盘上的文件大小:

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

这正是它所设置的在代码#2中与代码#1相同)。 –

+0

根据代码,这是目前用于$ ZipSize的内容。 – dux0r