Home > Backend Development > PHP Tutorial > Solve the problem of garbled Chinese file names when downloading PHP files

Solve the problem of garbled Chinese file names when downloading PHP files

WBOY
Release: 2016-07-25 08:55:36
Original
1194 people have browsed it
  1. $filename = "document.txt";
  2. header('Content-Type: application/octet-stream');
  3. header('Content-Disposition: attachment; filename=' . $ filename);
  4. print "Hello!";
  5. ?>
Copy the code

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:

  1. $filename = "Chinese file name.txt";
  2. header('Content-Type: application/octet-stream');
  3. header('Content-Disposition: attachment; filename= ' . $filename);
  4. print "Hello!";
  5. ?>
Copy the code

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:

  1. $filename = "Chinese file name.txt";
  2. $encoded_filename = urlencode($filename);
  3. $encoded_filename = str_replace("+", "%20", $encoded_filename) ;
  4. $ua = $_SERVER["HTTP_USER_AGENT"];
  5. header('Content-Type: application/octet-stream');
  6. if (preg_match("/MSIE/", $ua)) { // bbs.it -home.org
  7. header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
  8. } else if (preg_match("/Firefox/", $ua)) {
  9. header('Content -Disposition: attachment; filename*="utf8''' . $filename . '"');
  10. } else {
  11. header('Content-Disposition: attachment; filename="' . $filename . '"');
  12. }
  13. print 'ABC';
  14. ?>
Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template