首页 >php教程 >php手册 >PHP Header下载文件在IE文件名中文乱码问题

PHP Header下载文件在IE文件名中文乱码问题

WBOY
WBOY原创
2016-05-24 12:59:141293浏览

解决PHP Header下载文件在IE文件名中文乱码有两种常见的,一种是是把页面编码改成utf8,另一种是对中文url进入urlencode编码就可以解决了.

解决方案一,我的页面是utf-8编码,代码如下:

$filename = "中文.txt"; 
$ua = $_SERVER["HTTP_USER_AGENT"]; 
$encoded_filename = urlencode($filename); 
$encoded_filename = str_replace("+", "%20", $encoded_filename); 
header('Content-Type: application/octet-stream'); 
if (preg_match("/MSIE/", $ua)) { 
header('Content-Disposition: attachment; filename="' . $encoded_filename . '"'); 
} else if (preg_match("/Firefox/", $ua)) { 
header('Content-Disposition: attachment; filename*="utf8''' . $filename . '"'); 
} else { 
header('Content-Disposition: attachment; filename="' . $filename . '"'); 
}

解决方法二,将文件名先urlencode一下再放入header,如下.

<?php 
$file_name = urlencode($_REQUEST[&#39;filename&#39;]); 
header("Pragma: public"); header("Expires: 0"); 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Content-Type: application/force-download"); 
header(&#39;Content-Type: application/vnd.ms-excel; charset=utf-8&#39;); 
header("Content-Transfer-Encoding: binary"); 
//开源代码phprm.com 
header(&#39;Content-Disposition: attachment; filename=&#39;.$file_name); 
echo stripslashes($_REQUEST[&#39;content&#39;]);


本文链接:

收藏随意^^请保留教程地址.

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn