This article describes the PHP attachment download Chinese name garbled solution. Share to everyone for your reference, specific as follows:
In PHP, if the file name you want to download is Chinese, the file title will appear garbled.
At this point the need to encode the title, that is, advanced UrlEncode, and then put the header, and then solve the problem.
$filename = UrlEncode ("Download Document");
Header ("content-disposition:attachment; Filename= $filename. xls ");
On the internet, in the definition of RFC2231, the content-disposition of multi-language encoding should be defined as:
Copy Code code as follows:
Content-disposition:attachment; filename*= "UTF8 '%e6%b5%8b%e8%af%95.html"
That
Add * before the equals sign after filename
The value of filename is divided into three paragraphs in single quotes, which are character sets (UTF8), language (empty), and UrlEncode file names.
So at this time should be a URL code conversion of the file name, using PHP UrlEncode is easy to get it done
$ua = _server["Http_user_agent"];
$filename = "Chinese 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 help you with the PHP program design.