php中強制下載文件的代碼(解決了IE下中文文件名亂碼問題)
更新時間:2011年05月09日 19:15:44 作者:
以下這段代碼作用是:瀏覽器提交excel格式的數(shù)據(jù)和文件名到服務(wù)器上,PHP將請求轉(zhuǎn)化為可下載的excel文件,并要求瀏覽器彈出文件下載提示窗口
中間遇到一個問題是提交的中文文件名直接放到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實現(xiàn)遠程下載文件到本地
- php實現(xiàn)當前頁面點擊下載文件的實例代碼
- php下載文件源代碼(強制任意文件格式下載)
- php實現(xiàn)從ftp服務(wù)器上下載文件樹到本地電腦的程序
- php download.php實現(xiàn)代碼 跳轉(zhuǎn)到下載文件(response.redirect)
- PHP/ThinkPHP實現(xiàn)批量打包下載文件的方法示例
- PHP 下載文件時自動添加bom頭的方法實例
- PHP 下載文件時如何自動添加bom頭及解釋BOM頭和去掉bom頭的方法
- php實現(xiàn)SAE上使用storage上傳與下載文件的方法
- PHP實現(xiàn)從遠程下載文件的方法
- php+js實現(xiàn)的無刷新下載文件功能示例
相關(guān)文章
PHP中 empty() 和 isset() 的區(qū)別介紹
作為PHP中經(jīng)常用來判斷變量是否為空的函數(shù):empty()和isset() ,二者其實在很多方面還是存在區(qū)別的,本文將為大家詳細介紹一下他們的區(qū)別之處,需要的朋友可以了解一下2021-12-12實現(xiàn) win2003 下 mysql 數(shù)據(jù)庫每天自動備份
這篇文章主要為大家介紹下,如果用批處理實現(xiàn)mysql的自動備份,需要的朋友可以參考下2006-12-12PHP關(guān)于foreach復制知識點總結(jié)
在本篇文章里小編給大家分享了關(guān)于PHP關(guān)于foreach復制知識點總結(jié),有興趣的朋友們學習下。2019-01-01解析php中static,const與define的使用區(qū)別
本篇文章是對php中static,const與define的使用區(qū)別進行了詳細的分析介紹,需要的朋友參考下2013-06-06php常用字符串String函數(shù)實例總結(jié)【轉(zhuǎn)換,替換,計算,截取,加密】
這篇文章主要介紹了php常用字符串String函數(shù),結(jié)合實例形式總結(jié)分析了php常用字符串函數(shù)的功能與使用技巧,包括字符串的轉(zhuǎn)換、替換、計算、截取、加密等各種常用操作相關(guān)函數(shù),需要的朋友可以參考下2016-12-12