Home  >  Article  >  php教程  >  PHP Header下载文件在IE文件名中文乱码问题

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

WBOY
WBOYOriginal
2016-05-24 12:59:141235browse

解决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;]);


本文链接:

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

Statement:
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