用于下载文件的 PHP 脚本在 IE 中不起作用

发布于 2024-08-01 13:07:58 字数 784 浏览 0 评论 0原文

我有一个脚本,它从 $_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

揪着可爱 2024-08-08 13:07:59

切勿更换此:
标头(“内容类型:应用程序/八位字节流”);

有了这个:
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!

無心 2024-08-08 13:07:59

只是一个提示,如果有人(像我一样)在使用安全的 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.

勿忘心安 2024-08-08 13:07:59

将 Content-Disposition 标头设置为“attachment”,如下所示(在 PHP 中):附件是编写脚本的位置。

header('Content-Disposition: attachment');

并在 .htaccess 中添加以下内容,并添加您想要下载的扩展名,而不仅仅是 txt

<FilesMatch "\.(txt|pdf|csv|xls|xlsx|xlam|xlsb|xlsm|msg|doc|docx|mpg|jpg|png)">
   Header set Content-Disposition attachment
</FilesMatch>

Set the Content-Disposition header to "attachment", like so (in PHP): where attachment was scripted.

header('Content-Disposition: attachment');

And add the following in .htaccess and add what ever extension that you want to download not only txt

<FilesMatch "\.(txt|pdf|csv|xls|xlsx|xlam|xlsb|xlsm|msg|doc|docx|mpg|jpg|png)">
   Header set Content-Disposition attachment
</FilesMatch>
浪漫之都 2024-08-08 13:07:59

设法通过使用 php.net

https:// www.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;

Managed to get this working by using the first example from php.net

https://www.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;

德意的啸 2024-08-08 13:07:59

替换这个:
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.

执笏见 2024-08-08 13:07:59

要解决错误:“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文