解决 PHPHeader 下载文件在 IE 文件名中文乱码有两种常见的, 一种是是把页面编码改成 utf8, 另一种是对中文 url 进入 urlencode 编码就可以解决了. 解决方案一, 我的页面是 utf-8 编码...
解决 PHP Header 下载文件在 IE 文件名中文乱码有两种常见的, 一种是是把页面编码改成 utf8, 另一种是对中文 url 进入 urlencode 编码就可以解决了.
解决方案一, 我的页面是 utf-8 编码, 代码如下:
- $filename = "中文. txt";
- $ua = $_SERVER["HTTP_USER_AGENT"];
- $encoded_filename = urlencode($filename);
- $encoded_filename = str_replace("+", " ", $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");
- // 开源代码 phprm.com
- header('Content-Disposition: attachment; filename='.$file_name);
- echo stripslashes($_REQUEST['content']);
来源: https://www.php1.cn/detail/php-0efeb437cc.html