欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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']);
?>

相關(guān)文章

最新評論