简体   繁体   English

PHP强制在Internet Explorer中下载文件失败

[英]PHP Forced file download failing in Internet Explorer

I have a site where I'm using the following code to force a file download via PHP: 我在一个网站上使用以下代码来强制通过PHP下载文件:

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

While this works perfectly in Chrome and Firefox, it simply does nothing in Internet Explorer. 尽管这在Chrome和Firefox中完美运行,但在Internet Explorer中却无能为力。 While Googling I found a potential solution and changed the code to as follows: 在谷歌搜索时,我发现了一个可能的解决方案,并将代码更改为以下内容:

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

But still no luck. 但是仍然没有运气。 I don't know why it's failing, nor where to start looking for errors or problems, is this just some IE bug I'm unaware of? 我不知道为什么它失败了,也不知道从哪里开始寻找错误或问题,这只是我不知道的一些IE错误? Where should I start trying to find a solution? 我应该从哪里开始寻找解决方案?

Note: $ZipTitle will always be 'TexturePacker_Pack_xxx' where xxx is an incremented number. 注意:$ ZipTitle始终为'TexturePacker_Pack_xxx',其中xxx为递增数字。 $ZipFilename is an existing zip file which is unlinked AFTER the file is sent to the browser. $ ZipFilename是一个现有的zip文件,在将文件发送到浏览器后会取消链接。

Edit: The site and code in question are in action on http://www.texturepacker.net 编辑:有关站点和代码正在http://www.texturepacker.net上运行

Your Content-Length header seems to be incorrect. 您的Content-Length标头似乎不正确。 PHP filesize expects a string and returns an int. PHP filesize 需要一个字符串并返回一个int。

Change it to actually get the size of the file on disk: 更改它以实际获取磁盘上文件的大小:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM