解决PHP在IE上载文件,为文件名为乱码有关问题

WBOY
Release: 2016-06-13 13:02:05
Original
764 people have browsed it

解决PHP在IE下载文件,为文件名为乱码问题

通常使用以下代码就能导出为流的文件,而不是打开文件

header("Content-Type: application/force-download; charset=utf-8;name=\"$file_name\\");
header("Content-Transfer-Encoding: binary");
header("Content-Disposition: attachment;filename=\"$file_name\"");
Copy after login



但是,如果$file_name是UTF-8编码的,比如文件名为测试.html;
ie这时就搞些小情绪,文件名变成了乱码了

在RFC2231的定义里面, 多语言编码的Content-Disposition应该这么定义:
Content-Disposition: attachment; filename*="utf8''%E6%B5%8B%E8%AF%95.html"
即:
filename后面的等号之前要加 *
filename的值用单引号分成三段,分别是字符集(utf8)、语言(空)和urlencode过的文件名。
所以这时应该对文件名进行url编码转换 ,使用php的urlencode很轻松就搞定了

因此,以上代码应该加上url编码转换

$file_name = urlencode($file_name);
Copy after login






Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!