用于下载文件的 PHP 脚本在 IE 中不起作用
我有一个脚本,它从 $_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);
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.
$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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
切勿更换此:
标头(“内容类型:应用程序/八位字节流”);
有了这个:
header("内容类型:应用程序/强制下载");
“application/octet-stream”是最通用的并且适用于大多数浏览器。
我在一项测试中尝试使用“application/zip”,因为我在技术上处理 ZIP 文件,但 IE6.0 损坏了下载! 但其他一切都表现正常。 但是,是的,必须切换回“application/octet-stream”,因此任何尝试检测文件扩展名并切换到特定于扩展名的其他内容类型的代码都是有风险的! 您最好对所有二进制文件使用“application/octet-stream”!
Never replace this:
header("Content-type: application/octet-stream");
with this:
header("Content-Type: application/force-download");
"application/octet-stream" is simply the most universal and works on most browsers.
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!
只是一个提示,如果有人(像我一样)在使用安全的 https 请求直接将文件下载输入地址栏时遇到问题。 有一个 IE 错误导致此下载失败:
http://support.microsoft。 com/kb/323308/en-us
唯一的解决方法似乎是根据文章设置缓存标头。
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:
http://support.microsoft.com/kb/323308/en-us
Only workaround seems to be setting the cache-headers according to the article.
将 Content-Disposition 标头设置为“attachment”,如下所示(在 PHP 中):附件是编写脚本的位置。
并在 .htaccess 中添加以下内容,并添加您想要下载的扩展名,而不仅仅是 txt
Set the Content-Disposition header to "attachment", like so (in PHP): where attachment was scripted.
And add the following in .htaccess and add what ever extension that you want to download not only txt
设法通过使用 php.net
https:// www.php.net/manual/en/function.readfile.php
Managed to get this working by using the first example from php.net
https://www.php.net/manual/en/function.readfile.php
替换这个:
header("内容类型:应用程序/八位字节流");
与此:
header("Content-Type: application/force-download");
根据此 post,IE 通常不会监听您的标头,而是自行查找您发送的内容。
Replace this:
header("Content-type: application/octet-stream");
with this:
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.
要解决错误:“Internet Explorer 无法从 www.example.com 下载 download.php”,
将这些标头添加到您的脚本中:
header("Pragma: ");
header("Cache-Control: ");
该代码将从标头中删除 Cache-Control导致下载出现问题。
上面的代码应该添加到文件的顶部。
它对我们来说效果很好。
To solve the error : "Internet Explorer cannot download download.php from www.example.com",
Add these headers to your script:
header("Pragma: ");
header("Cache-Control: ");
The code will remove the Cache-Control from headers which makes the download problem.
The above code should be added at the top of the file.
It works fine for us.