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

2022-01-17 00:00:00 http-headers php

我有一个脚本,它从 $_GET['key'] 获取密钥,在数据库中查找位置,并使用 readfile 和一些标题来提供下载以供使用.这适用于 Firefox 但不适用于 IE8,无法在另一个 IE 上进行测试.我在 IE 中收到以下错误:Internet Explorer 无法从 www.example.com 下载 download.php".好像它正在尝试下载 PHP 脚本.

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

推荐答案

解决错误:Internet Explorer 无法从 www.example.com 下载 download.php",将这些标头添加到您的脚本中:

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: ");

代码将从导致下载问题的标头中删除 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.

它对我们来说很好.

相关文章