This article mainly introduces the solution of garbled Chinese names for PHP attachment downloading, analyzes the causes of garbled Chinese characters and the corresponding encoding conversion methods, which has some reference value, for more information about how to download PHP attachments, see the example below. We will share this with you for your reference. The details are as follows:
In PHP, if the file name to be downloaded is called Chinese, the file title is garbled.
In this case, you need to encode the title, that is, urlencode, and then put the header. then the problem is solved.
$ Filename = urlencode (""); header ("Content-disposition: attachment; filename=$filename.xls ");
According to the Internet, in RFC2231 definition, the Content-Disposition of multi-language encoding should be defined as follows:
The code is as follows:
Content-Disposition: attachment; filename * = "utf8'%e6%b5%8b%e8%af%95.html"
That is:
Add the equal sign after filename *
The filename value is divided into three sections by single quotes, which are character set (utf8), Language (null), and URL code file names.
Therefore, url encoding should be performed on the file name. using php's urlencode can be easily completed.
$ 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 will help you with PHP programming.