: This article describes how to download garbled Chinese names for PHP attachments. For more information about PHP tutorials, see. This example describes how to download PHP attachments with garbled Chinese names. 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, the file name should beUrl encodingConversion: It's easy to use php's urlencode.
$ 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.
The above describes how to download garbled Chinese names from PHP attachments, including url encoding, and hope to help anyone interested in PHP tutorials.