下载文件时safari上中文文件名乱码的处理办法(php代码实现)

php一段代码,处理文件下载时的文件名,后来客户反馈说firefox,safari下的中文文件名不能正常显示。

        $file_info['title'] = rawurlencode($file_info['title']);
        $filename=iconv('utf-8', 'gbk', $file_info['title']);
        header("Content-Disposition:attachment;filename = " . $filename);

修正为以下内容后firefox,safari下正常显示

如果没有兴趣请看简版,一句话解决
if (preg_match("/Safari/", $ua)) { 
header( 'Content-Disposition: attachment; filename*="UTF-8\'\'' .  rawurlencode($file_info['title']) );
}

之前试过和firefox一样处理方法,结果下载的文件名连扩展名都没有了
 header( 'Content-Disposition: attachment; filename*=UTF-8\'\'' .  rawurlencode($file_info['title'])  . '"' ); 

兜兜转转后发现去掉一对双引号就可以,

求此时此刻内心的阴影面积

一堆代码参考一下。


        $file_info['title'] = rawurlencode($file_info['title']);
        $filename=iconv('utf-8', 'gbk', $file_info['title']);
//        header("Content-Disposition:attachment;filename = " . $filename);


        $encoded_filename =  $file_info['title'] 
        $ua = $_SERVER["HTTP_USER_AGENT"];
        //IE
        if(preg_match("/MSIE/", $ua) || preg_match("/Trident\/7.0/", $ua)){
            header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
        } else if (preg_match("/Firefox/", $ua)) {
            header('Content-Disposition: attachment; filename*="utf8\'\'' . $filename . '"');
        } else if (preg_match("/Safari/", $ua)) {
            header( 'Content-Disposition: attachment; filename*=UTF-8\'\'' . $encoded_filename );
        } else {
            header('Content-Disposition: attachment; filename="' . $filename . '"');
        }

相关原理有请度娘上场

浏览器能正确识别的编码格式,只要按照这样的编码来设置对应的Content-Disposition,那么应该就不会出现中文文件名的乱码问题了。 
首先,Content-Disposition值可以有以下几种编码格式 
1. 直接urlencode: 

    Content-Disposition: attachment; filename=”struts2.0%E4%B8%AD%E6%96%87%E6%95%99%E7%A8%8B.chm” 

2. Base64编码: 

    Content-Disposition: attachment; filename=”=?UTF8?B?c3RydXRzMi4w5Lit5paH5pWZ56iLLmNobQ==?=” 

3. RFC2231规定的标准: 

    Content-Disposition: attachment; filename*=UTF-8”%E5%9B%9E%E6%89%A7.msg 

4. 直接ISO编码的文件名: 

    Content-Disposition: attachment;filename=”测试.txt” 

然后,各浏览器支持的对应编码格式为: 

1.  IE浏览器,采用URLEncoder编码 
2.  Opera浏览器,采用filename*方式 
3.  Safari浏览器,采用ISO编码的中文输出 
4.  Chrome浏览器,采用Base64编码或ISO编码的中文输出 
5.  FireFox浏览器,采用Base64或filename*或ISO编码的中文输出 

new_filename = URLEncoder.encode(filename, “UTF8”);  
// 如果没有UA,则默认使用IE的方式进行编码,因为毕竟IE还是占多数的  
rtn = “filename=\”” + new_filename + “\””;  
if (userAgent != null)  
{  
     userAgent = userAgent.toLowerCase();  
      // IE浏览器,只能采用URLEncoder编码  
     if (userAgent.indexOf(“msie”) != -1)  
    {  
        rtn = “filename=\”” + new_filename + “\””;  
    }  
     // Opera浏览器只能采用filename*  
     else if (userAgent.indexOf(“opera”) != -1)  
     {  
        rtn = “filename*=UTF-8”” + new_filename;  
    }  
    // Safari浏览器,只能采用ISO编码的中文输出  
      else if (userAgent.indexOf(“safari”) != -1 )  
      {  
          rtn = “filename=\”” + new String(filename.getBytes(“UTF-8″),”ISO8859-1”) + “\””;  
      }  
      // Chrome浏览器,只能采用MimeUtility编码或ISO编码的中文输出  
      else if (userAgent.indexOf(“applewebkit”) != -1 )  
       {  
         new_filename = MimeUtility.encodeText(filename, “UTF8”, “B”);  
          rtn = “filename=\”” + new_filename + “\””;  
       }  
      // FireFox浏览器,可以使用MimeUtility或filename*或ISO编码的中文输出  
       else if (userAgent.indexOf(“mozilla”) != -1)  
       {  
          rtn = “filename*=UTF-8”” + new_filename;  
      }  
   }  

ref:

https://my.oschina.net/u/1391970/blog/215361

https://blog.csdn.net/weixin_30701575/article/details/97463481

https://my.oschina.net/junn/blog/97699

https://bbs.csdn.net/topics/390424956 最后解决的来源

header( ‘Content-Disposition: attachment; filename*=UTF-8\’\” . rawurlencode ( $originfile ) );