本文實(shí)例講述了PHP附件下載中文名稱(chēng)亂碼的解決方法。分享給大家供大家參考,具體如下:
PHP中,如果要下載的文件名稱(chēng)為中文,則會(huì)出現(xiàn)文件標(biāo)題亂碼。
此時(shí)就需要對(duì)標(biāo)題進(jìn)行編碼,也就是說(shuō)先進(jìn)性u(píng)rlencode,然后再放入header,然后問(wèn)題就解決了。
$filename = urlencode("下載文檔");header ( "Content-disposition: attachment; filename=$filename.xls" ); 網(wǎng)上說(shuō),在RFC2231的定義里面, 多語(yǔ)言編碼的Content-Disposition應(yīng)該這么定義:
即:
filename后面的等號(hào)之前要加 *
filename的值用單引號(hào)分成三段,分別是字符集(utf8)、語(yǔ)言(空)和urlencode過(guò)的文件名。
所以這時(shí)應(yīng)該對(duì)文件名進(jìn)行url編碼轉(zhuǎn)換 ,使用php的urlencode很輕松就搞定了
$ua = _SERVER["HTTP_USER_AGENT"];$filename = "中文 文件名.txt";$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 . '"');} 希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

















