duanjue2560 2013-08-06 14:05
浏览 47

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

So I've read through about every IE download script error, and tried almost every solution. This is the final code I've ended up with from here, however it still doesn't fix my problem.

my links are like: http://www.example.com/download.php?file=example.html

My download script works in every browser I've tried it in. However, when I test in IE8 on a fresh machine or with the cache cleared, the first time I attempt to download I get: "IE cannot download file download.php from example.com...".

Now if I hit enter again on it, the file download works perfect. No issues, file is found and is downloadable. But if I clear the cache or try it on another machine in the lab with IE8 I haven't done it on before, it throws the error again the first time. Just the first time, then after that it's fine. I'm tearing my hair out, please help. Here's my code...

//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条回答 默认 最新

  • douzhuan0309 2013-08-06 14:08
    关注

    there is an error in your code. Use

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

    or

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

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line