dqqy64515 2011-04-06 05:46
浏览 33
已采纳

使用PHP发送文件时,在Internet Explorer中维护自定义文件扩展名?

I have a server-side zip archive that I would like to pass along as a download. I'm using a custom extension to associate these specific archives with some client software (e.g. CustomArchive.bwz).

Chrome and FireFox handle this custom extension perfectly, but Internet Explorer keeps tacking ".zip" at the end of the file name (e.g. CustomArchive.bwz.zip).

Here is my download file method:

function downloadFile($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, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}

As an example, I am calling it like this:

downloadFile( "CustomArchive.bwz" );

Where CustomArchive.bwz is simply a zip file with a custom extension.

After playing with removing various bits from my downloadFile method, I was able to narrow it down to my calling ob_clean(). Unfortunately, removing that call causes the downloads to be corrupted.

Is there a work around that I can force IE to use the .bwz extension and not tack on .zip?

My server is running PHP 5.3.5 on IIS 7.5.

  • 写回答

1条回答 默认 最新

  • duanping1632 2011-04-06 06:00
    关注

    From marcelebrate: using application/force-download instead of application/octet-stream is what prevented IE from assuming my file was a zip archive.

    function downloadFile($file){
        $file_name = $file;
        $mime = 'application/force-download';
    
        header('Pragma: public');   // required
        header('Expires: 0');       // no cache
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Cache-Control: private',false);
        header('Content-Type: '.$mime);
        header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
        header('Content-Transfer-Encoding: binary');
        header('Connection: close');
        header('Content-Length: ' . filesize($file));
    
        //from Christian Sciberras: There may (often) be multiple levels of output buffering.
        while(ob_get_level()) ob_end_clean();
        ob_implicit_flush(true);
    
        readfile($file_name);       // push it out
        exit();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大