PHP: Force file download and IE, yet again

后端 未结 4 1997
猫巷女王i
猫巷女王i 2020-11-28 10:03

Folks, I know there have been lots of threads about forcing the download dialog to pop up, but none of the solutions worked for me yet.

My app sends mail to the use

相关标签:
4条回答
  • 2020-11-28 10:35

    Your no-cache directives (Vary, Expires, Pragma) are causing the problem.

    See http://blogs.msdn.com/ieinternals/archive/2009/10/02/Internet-Explorer-cannot-download-over-HTTPS-when-no-cache.aspx

    0 讨论(0)
  • 2020-11-28 10:43

    This will check for versions of IE and set headers accordingly.

    // assume you have a full path to file stored in $filename
    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));
    
    // use this unless you want to find the mime type based on extension
    $mime = array('application/octet-stream');
    
    header('Content-Type: '.$mime);
    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 (preg_match('~MSIE|Internet Explorer~i', $_SERVER['HTTP_USER_AGENT']) || (strpos($_SERVER['HTTP_USER_AGENT'], 'Trident/7.0; rv:11.0') !== 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);
    
    0 讨论(0)
  • 2020-11-28 10:49

    If you're trying to get the file to download every time, change the content type to 'application/octet-stream'.

    Try it without the pragma statement.

    0 讨论(0)
  • 2020-11-28 10:59

    Just use:

    header('Content-Disposition: attachment');
    

    That's all. (Facebook does the same.)

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