Home > Article > Backend Development > Solve the problem of garbled Chinese file names when downloading PHP files
After opening it with a browser, you can download document.txt. However, if $filename is UTF-8 encoded, some browsers cannot handle it properly. Example:
Save the program in UTF-8 encoding and access it again, the file name downloaded by IE6 will be garbled. The file name downloaded under FF3 only has the word "Chinese". Everything works fine under Opera 9. Output header: Content-Disposition: attachment; filename=中文 filename.txt In fact, according to the definition of RFC2231, the Content-Disposition of multi-language encoding should be defined like this: Content-Disposition: attachment; filename*="utf8''%E4%B8%AD%E6%96%87%20%E6%96%87%E4%BB%B6%E5%90%8D.txt" Right now: The equal sign after filename must be preceded by *. The value of filename is divided into three segments in single quotes, which are character set (utf8), language (empty) and urlencoded file name. It is best to add double quotes, otherwise the part after the space in the file name It doesn't show up in Firefox. Note that the result of urlencode is not the same as the result of php's urlencode function. PHP's urlencode will replace spaces with +, which needs to be replaced with %20 here. After testing, it was found that the support of several mainstream browsers is as follows:
|