用于下载文件的 PHP 脚本在 IE 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1218925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP script to download file not working in IE
提问by Stuart
I have a script that takes a key from $_GET['key'] , looks up the location in a database and uses the readfile together with some headers to present a download for the use. This works in Firefox but not IE8, haven't been able to test it on another IE. I get the following error in IE: "Internet Explorer cannot download download.php from www.example.com". As if it is trying to download the PHP script.
我有一个脚本,它从 $_GET['key'] 获取一个密钥,在数据库中查找位置,并使用 readfile 和一些标题来提供下载以供使用。这适用于 Firefox,但不适用于 IE8,无法在另一个 IE 上对其进行测试。我在 IE 中收到以下错误:“Internet Explorer 无法从 www.example.com 下载 download.php”。就好像它正在尝试下载 PHP 脚本一样。
$the_query = "SELECT * FROM `files` WHERE `user_id`=" . $_SESSION['user_id'] . " AND `key`='" . $key . "'";
$result = mysql_query($the_query);
$row = mysql_fetch_array($result);
$file = '/var/www/vhosts/www.example.com/httpsdocs/uploads/' . $row['id'] . '/' . $row['file'];
header("Content-type: application/octet-stream");
header("Content-length: ".filesize($file));
header('Content-Description: File Transfer');
header("Cache-control: private");
header('Content-Disposition: attachment; filename=' . rawurlencode(basename($file)));
readfile($file);
回答by SequenceDigitale.com
To solve the error : "Internet Explorer cannot download download.php from www.example.com", Add these headers to your script:
要解决错误:“Internet Explorer 无法从 www.example.com 下载 download.php”,请将这些标头添加到您的脚本中:
header("Pragma: ");
header("Pragma: ");
header("Cache-Control: ");
header("Cache-Control: ");
The code will remove the Cache-Control from headers which makes the download problem.
该代码将从导致下载问题的标头中删除 Cache-Control。
The above code should be added at the top of the file.
上面的代码应该添加在文件的顶部。
It works fine for us.
它对我们来说很好用。
回答by Stuart
Managed to get this working by using the first example from php.net
通过使用来自 php.net 的第一个示例设法使其正常工作
http://us3.php.net/manual/en/function.readfile.php
http://us3.php.net/manual/en/function.readfile.php
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
回答by Tyler Carter
Replace this:header("Content-type: application/octet-stream");
with this:header("Content-Type: application/force-download");
用这个替换header("Content-type: application/octet-stream");
:header("Content-Type: application/force-download");
According to this post, IE doesn't normally listen to your headers, and instead looks for itself what you are sending.
根据这篇文章,IE 通常不会侦听您的标头,而是自行查找您发送的内容。
回答by acme
Just a hint, if someone (like me) is facing problems with directly entering a filedownload into the address bar using a secured https-request. There is a IE bug that is causing this download to fail:
只是一个提示,如果有人(像我一样)在使用安全的 https 请求将文件下载直接输入到地址栏中时遇到问题。有一个 IE 错误导致此下载失败:
http://support.microsoft.com/kb/323308/en-us
http://support.microsoft.com/kb/323308/en-us
Only workaround seems to be setting the cache-headers according to the article.
唯一的解决方法似乎是根据文章设置缓存头。
回答by John Doe
Never replace this: header("Content-type: application/octet-stream");
永远不要替换这个: header("Content-type: application/octet-stream");
with this: header("Content-Type: application/force-download");
用这个: header("Content-Type: application/force-download");
"application/octet-stream" is simply the most universal and works on most browsers.
“application/octet-stream”是最通用的,适用于大多数浏览器。
I tried using "application/zip" in one of my tests since I was technically dealing with a ZIP file, but IE6.0 corrupted the download! Everything else behaved normally though. But yeah, had to switch back to "application/octet-stream" so any code out there that tries to detect the file extension, and switch to other content-types specific to the extension are risky! You're better off using "application/octet-stream" for ALL binary files!
我尝试在我的一项测试中使用“application/zip”,因为我在技术上处理的是 ZIP 文件,但 IE6.0 损坏了下载!不过其他一切都表现正常。但是,是的,必须切换回“应用程序/八位字节流”,因此任何尝试检测文件扩展名并切换到特定于扩展名的其他内容类型的代码都是有风险的!您最好对所有二进制文件使用“应用程序/八位字节流”!

