This article describes the PHP attachment download Chinese name garbled solution. Share to everyone for your reference, as follows:
In PHP, if the file name you want to download is in Chinese, the file header will appear garbled.
At this point, you need to encode the title, that is, advanced UrlEncode, and then put in the header, and then solve the problem.
$filename = UrlEncode ("Download Document"); header ("Content-disposition:attachment; Filename= $filename. xls ");
Online, in the definition of RFC2231, the multi-language encoding of content-disposition should be defined as:
Copy the 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 segments with single quotation marks, namely the character set (UTF8), language (empty), and UrlEncode file names.
So this should be a URL encoding file name conversion, the use of PHP UrlEncode is easy to take care of
$ua = _server["Http_user_agent"]; $filename = "Chinese file name: 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 is helpful to you in PHP programming.
The above describes the PHP attachment download Chinese name garbled solution, including the content of the URL encoding, I hope the PHP tutorial interested in a friend helpful.