Solve the problem of garbled Chinese file names during php file download-php Tutorial

Source: Internet
Author: User
Solve the problem of garbled Chinese file names during php file download

  1. $ Filename = "document.txt ";
  2. Header ('content-Type: application/octet-stream ');
  3. Header ('content-Disposition: attachment; filename = '. $ filename );
  4. Print "Hello! ";
  5. ?>

After a browser is opened, you can download document.txt.

However, if $ filename is UTF-8 encoded, some browsers won't be able to handle it.

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. ?>

Save the program as a UTF-8 code and then access, IE6 download file name will be garbled. The file name downloaded under FF3 is only in Chinese. Everything works normally in Opera 9.

Output header: Content-Disposition: attachment; filename = Chinese file name .txt

In fact, according to the RFC2231 definition, the Content-Disposition of multi-language encoding should be defined as follows: Content-Disposition: attachment; filename * = "identifier" that is: before the equal sign after filename, add the * filename value and use single quotation marks to divide it into three sections, namely character set (utf8), Language (null), and URL code file names. It is best to add double quotation marks. Otherwise, the content after the space in the file name cannot be displayed in Firefox.

Note that the urlencode result is not the same as the urlencode function result of php. The urlencode of php replaces the space with +, and the urlencode must be replaced with % 20 here.

After experiments, we found that the support for 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. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.