使用PHP标头在IE8中强制下载无效

时间:2014-04-26 08:27:06

标签: php

我使用下面的代码使用PHP标题下载文件,它在其他浏览器中工作正常但它不能在IE8中工作。在IE8中没有显示下载提示,并且还会自动打开新窗口,然后突然关闭。

$('#download').click(function()){
  window.open('Download.php');
});

的download.php

session_start();
$file = $_SESSION['DFN'];   
if (file_exists($file)) {
    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');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
}

1 个答案:

答案 0 :(得分:0)

    if ($file) {

      $dir      = "../uploads/";        

    $mime_types =   array(
        "pdf" => "application/pdf",
        "txt" => "text/plain",
        "zip" => "application/zip",
        "doc" => "application/msword",
        "gif" => "image/gif",
        "png" => "image/png",
        "jpeg"=> "image/jpg",
        "jpg" =>  "image/jpg",
        "mp3" => "audio/mpeg",
        "mpeg" => "audio/mpeg",
        "swf"=> "application/x-shockwave-flash",
        "rar" =>  "application/x-rar-compressed",       
        "mp4" => "audio/mp4"
     );


    if ((isset($file))&&(file_exists($dir.$file))) {

       header("Content-type: " . $mime_types[get_file_extention($file)]);

       header('Content-Disposition: inline; filename="' . $dir.$file . '"');
       header("Content-Transfer-Encoding: Binary");
       header("Content-length: ".filesize($dir.$file));
       header('Content-Type: application/octet-stream');
       header('Content-Disposition: attachment; filename="' . $file . '"');

       readfile("$dir$file");
    }

}