File download in IE6 gets the wrong filename

前端 未结 2 393
清歌不尽
清歌不尽 2020-12-19 12:09

I have a PHP script being served over https:// that is trying to push a PDF file to the user. The problem that in Internet Explorer 6 (which unfortunately I still have to s

相关标签:
2条回答
  • 2020-12-19 12:30

    I think you might need a space after attachment;:

    header("Content-Disposition: attachment; filename=$filename");
    

    If that doesn't work, check out some of the points in this article, including:

    • On IE 6.0, things mostly work, but if you ALSO setup Cache-Control: no-cache, your suggested filename (and type!) will be IGNORED. A bummer if you have to choose between security and convienence. Of course, security wins.
    • On nearly all versions of IE, including 6.0, sometimes the browser will use the filename in the address bar instead of the Content-Disposition Header, and with IE5.5SP2 you're expected to change the UseCDFileName registry key, see Q303750. This was fixed with IE6.0SP1.

    EDIT: Here's the code I use, directly copied from my application's source. Let me know if this works any better...

    function forceDownload($filename,$mime=false,$downloadName=false)
    {
        if(file_exists($filename) && is_readable($filename))
        {
            if(!$mime)      $mime = DFStdLib::determineMimeType($filename);
    
            if(!$expire)    $expire = DFStdLib::HOUR_IN_SECONDS;
    
            if(!$downloadName) $downloadName = basename($filename);
    
            header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($filename)).' GMT', true, 200);
            header('Cache-Control: no-cache',true);
            header('Pragma: Public',true);
            header('Expires: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT',true);
            header('Content-Length: '.filesize($filename),true);
            header("Content-Type: {$mime}",true);
            header("Content-disposition: attachment; filename=$downloadName",true);
            readfile($filename);
            exit();
        }
        else
        {
            header('HTTP/1.1 404 Not Found',true,404);
            echo "<html><head><title>Not Found</title></head><body>The file was not found.</body></html>";
            exit();
        }
    }
    

    Usage for your case would be:

    forceDownload('/example.pdf','application/pdf','quickquote.pdf');
    

    Also you'll need to change DFStdLib::HOUR_IN_SECONDS to 3600 and write your own determineMimeType function, or delete that line and make the $mime argument required...

    0 讨论(0)
  • 2020-12-19 12:31

    You can use the following Content-Type to force IE to use the Content-Disposition header.

    (Work in IE7 when you choose to 'open' a file)

    Content-Type: application/save
    

    cf. http://blog.mjjames.co.uk/2009/04/content-disposition-in-different.html

    0 讨论(0)
提交回复
热议问题