PHP下载脚本在IE8中抛出错误

时间:2013-08-06 14:05:14

标签: php internet-explorer-8 download

所以我已经阅读了每个IE下载脚本错误,并尝试了几乎所有的解决方案。这是我从here得到的最终代码,但它仍然无法解决我的问题。

我的链接如下:http://www.example.com/download.php?file=example.html

我的下载脚本适用于我尝试过的每个浏览器。但是,当我在新机器上测试IE8或清除缓存时,我第一次尝试下载时得到:“IE无法下载文件下载来自example.com的.php ......“

现在,如果我再次点击输入,文件下载工作完美。没有问题,找到文件并且可以下载。但是如果我清除缓存或在实验室中用IE8在另一台机器上尝试它我之前没有完成它,它会在第一次再次抛出错误。这是第一次,之后就没事了。我正在撕开我的头发,请帮忙。这是我的代码......

//ini_set('error_reporting', E_ALL);
//ini_set('display_errors', 1);
set_time_limit(0);

$filepath = '/var/www/html/securitycoverage.com/scportal/redirect/www.securitycoverage.com/main/pages/';

$filename = $filepath.$_GET['file'];

if (!is_file($filename)) {
  die('The file appears to be invalid.');
}

$filepath = str_replace('\\', '/', realpath($filename));
$filesize = filesize($filepath);
$filename = substr(strrchr('/'.$filepath, '/'), 1);
$extension = strtolower(substr(strrchr($filepath, '.'), 1));

header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.sprintf('%d', $filesize));
header('Expires: 0');

// check for IE only headers
if (isset($_SERVER['HTTP_USER_AGENT']) && (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false)) {
  header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  header('Pragma: public');
} else {
  header('Pragma: no-cache');
}

$handle = fopen($filepath, 'rb');
fpassthru($handle);
fclose($handle);

1 个答案:

答案 0 :(得分:2)

您的代码中存在错误。使用

$mime = array('application/octet-stream');
header('Content-Type: '.$mime[0]);

header('Content-Type: application/octet-stream');