Iefans,靠谱的软件下载站

首页 最新文章 最新安卓 最新苹果 浏览器 IE教程 在线图片编辑 最新软件 最新游戏

当前位置: IEfans / IE专区 / IE动态/ php中强制下载文件的代码(解决了IE下中文文件名乱码问题)

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

编辑:秩名2023-06-28 14:00:02

中间遇到一个问题是提交的中文文件名直接放到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中文件下载功能实现超详细流程分析
  • php实现文件下载(支持中文文名)
  • 浏览器PHP下载文件名中的中文乱码问题解决方法
  • php 下载保存文件保存到本地的两种实现方法
  • PHP实现远程下载文件到本地
  • php实现文件下载代码分享
  • php下载文件的代码示例
  • PHP文件下载类
  • php实现支持中文的文件下载功能示例
返回旧版Copyright © 1998-2023 www.iefans.net All Rights Reserved 沪ICP备17019769号-18 意见反馈