虞大胆的PHP之旅

浏览器下载中文名文件出现乱码,如何解决?

2019-04-02  本文已影响49人  虞大胆的叽叽喳喳

《web开发中,如何让浏览器下载文件?》 中介绍了如何让浏览器下载文件的原理,其实非常简单,本质上就是输出一个 HTTP header 头(其实是一个 MIME header 头),但对于中国开发者来说,更实际的问题是:在某些浏览器上,如果下载的文件是中文名,那很有可能出现乱码。

实际上导致这一乱现的并不是技术问题,而是标准的问题,Web 发展到如今,经历了太多的混乱,为了把乱码这个问题说清楚,我花了一天时间,寻找了一些相关资料。

先上图吧,这是我在 google 上找到的:

20-browser-rfc.png

这个图由三部分 RFC 组成,分别是 MIME、Content-Disposition HEADER、HTTP。

首先 Content-Disposition 这个头由 MIME 定义,rfc2231(MIME Parameter Value and Encoded Word Extensions:Character Sets, Languages, and Continuations)是最新的标准,描述了 MIME 中字符集及语言的标准,废弃了 rfc2184 和 rfc204。

rfc2183 基于 rfc2231 定义了在互联网上 Content-Disposition 头的标准,说明了字符集及编码,可以这样理解 rfc2183 和 rfc2231 是从不同角度建立的标准,双方并不冲突。

现在来说 HTTP,因为乱码问题是在浏览器中产生的,也就是说肯定和 HTTP 协议有关,rfc2616 是 HTTP/1.1 协议的总标准(最新标准已经分为6个了),对于 HTTP 协议来说,header 只能是 ASCII,不会遇到也没有中文的问题,但为了支持下载,它借用了 MIME 的 Content-Disposition header。

在早期,HTTP 协议建议使用 rfc2047 标准(查看上图,很早的一个 MIME 标准),但浏览器没有按照该标准执行,这个时候出现了混乱。

If a character set other than ISO-8859-1 is used, it MUST be encoded in the warn-text using the method described in RFC 2047

不同浏览器为了解决乱码问题,提出了很多不一致的解决方案,比如对 Content-Disposition 中的 filename 值进行 base64 编码或百分号编码。

这个时候可苦了程序员,为了适应不同类型的浏览器,他们需要判断 UA,针对不同 UA 输出不同的 Content-Disposition,现在我们公司很多代码也是这样的编写方式。

那么有没有好的解决方案呢,rfc5987 发布了(基于 rfc2231、rfc2183),它严格定义了在 HTTP 协议中如何标准化编码 header 头;进一步 HTTP 定义了 rfc6266,它进一步标准化了在 HTTP 中如何编码 Content-Disposition,可以认为 rfc5987 和 rfc6266 是一样的。

RFC 1806 , from which the often implemented Content-Disposition header in HTTP is derived, has a number of very serious security considerations. Content-Disposition is not part of the HTTP standard, but since it is widely implemented, we are documenting its use and risks for implementors.

那 rfc5987 如何定义编码标注的呢?建议采用 parameter*=charset'lang'value 的格式。如下:

ext-value     = charset  "'" [ language ] "'" value-chars
charset       = "UTF-8" / "ISO-8859-1" / mime-charset
language      = <Language-Tag>
value-chars   = *( pct-encoded / attr-char )
pct-encoded   = "%" HEXDIG HEXDIG

对应到 Content-Disposition,就是输出下面的头:

Content-Disposition: attachment; filename="%E6%B3%95%E9%99%A2%E9%A1%B9%E7%9B%AE-%E7%B3%BB%E7%BB%9F%E6%94%AF%E6%8C%81.txt"; filename*=UTF-8''%22UTF-8%27%27%25E6%25B3%2595%25E9%2599%25A2%25E9%25A1%25B9%25E7%259B%25AE-%25E7%25B3%25BB%25E7%25BB%259F%25E6%2594%25AF%25E6%258C%2581.txt%22

新标准就是使用 filename* 代替 filename 参数,注意 UTF-8'' 后面的文件名使用百分号编码(在 PHP 中使用 rawurlencode() 函数)。为了兼容老的浏览器,同时也建议包含 filename 参数,对于这些老的浏览器来说,会忽略 filename* 这个“不标准的” field。而如果 filename 和 filename* 同时出现,较新的浏览器会忽略 filename 参数。

不过,就我的测试,在 IE 浏览器中,还是要根据 UA 输出只包含 filename field 的参数,光输出 filename* field 没用。

以下就是完整的示例:

$ua = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
$filename = rawurlencode($filename);

if (stripos($ua, 'msie') !== false || (stripos($ua, 'rv:11') !== false )) {
  header('Content-Disposition: attachment; filename="'.$filename.'"');
} else {
  header('Content-Disposition: attachment; filename="'.$filename.'";' . 'filename*="UTF-8\'\'' .  $filename . '"' );
} 

建议阅读:


推荐大家关注我的公众号(ID:yudadanwx,虞大胆的叽叽喳喳)和我的书《深入浅出HTTPS:从原理到实战》

上一篇 下一篇

猜你喜欢

热点阅读