The example in this article describes the solution to the garbled Chinese name of PHP attachment download. Share it with everyone for your reference, the details are as follows:
In PHP, if the name of the file to be downloaded is Chinese, the file title will be garbled.
At this point, you need to encode the title, which means advanced urlencode, and then put in the header, and then the problem is solved.
$filename = urlencode("下载文档"); header ( "Content-disposition: attachment; filename=$filename.xls" );
It is said online that in the definition of RFC2231, the Content-Disposition of multi-language encoding should be defined like this:
Copy the code The code is as follows:
Content-Disposition: attachment; filename*="utf8' '%E6%B5%8B%E8%AF%95.html"
Before the equal sign after filename, add *
The value of filename is divided into three sections with single quotes, which are character sets (utf8) , language (empty) and urlencoded file name.
So at this time, the file name should be url encodedconverted. It can be easily done using php's 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 . '"'); }
I hope this article will be helpful to everyone in PHP programming.
The above introduces the solution to the garbled Chinese name of PHP attachment download, including the URL encoding. I hope it will be helpful to friends who are interested in PHP tutorials.