php中强制下载文件的代码解决了IE下中文文件名乱码问题

中间遇到一个问题是提交的中文文件名直接放到header里在IE下会变成乱码,解决方法是将文件名先urlencode一下再放入header,如下。
<div class="codetitle"><a style="CURSOR: pointer" data="85993" class="copybut" id="copybut85993" onclick="doCopy('code85993')"> 代码如下:

<div class="codebody" id="code85993">
<?PHP
$file_name = urlencode($_REQUEST['filename']);
header("Pragma: public"); header("Expires: 0");
header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
header("Content-Type: application/force-download");
header('Content-Type: application/vnd.ms-excel; charset=utf-8');
header("Content-transfer-encoding: binary");
header('Content-disposition: attachment; filename='.$file_name);
echo stripslashes($_REQUEST['content']);
?>
解决PHP Header下载文件在IE文件中文乱码有两种常见的,一种是是把页面编码改成utf8,另一种是对中文url进入urlencode编码就可以解决了。
解决方案一(我的页面是utf-8编码): <div class="codetitle"><a style="CURSOR: pointer" data="77707" class="copybut" id="copybut77707" onclick="doCopy('code77707')"> 代码如下:
<div class="codebody" id="code77707">
$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,如下。
代码如下: <div class="codetitle"><a style="CURSOR: pointer" data="55681" class="copybut" id="copybut55681" onclick="doCopy('code55681')"> 代码如下:
<div class="codebody" id="code55681"> <?PHP
$file_name = urlencode($_REQUEST['filename']);
header("Pragma: public"); header("Expires: 0");
header("Cache-Control: must-revalidate,pre-check=0");
header("Content-Type: application/force-download");
header('Content-Type: application/vnd.ms-excel; charset=utf-8');
header("Content-transfer-encoding: binary");
header('Content-disposition: attachment; filename='.$file_name);
echo stripslashes($_REQUEST['content']);
?>

强制下载

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...