php中强制下载文件的代码(解决了IE下中文文件名乱码问题)
程序员文章站
2022-12-26 09:31:34
中间遇到一个问题是提交的中文文件名直接放到header里在ie下会变成乱码,解决方法是将文件名先urlencode一下再放入header,如下。 复制代码 代码如下: &l...
中间遇到一个问题是提交的中文文件名直接放到header里在ie下会变成乱码,解决方法是将文件名先urlencode一下再放入header,如下。
<?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编码):
$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['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编码):
复制代码 代码如下:
$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['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']);
?>
$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新手入门学习方法