Php implements download of Chinese file names

Source: Internet
Author: User
When downloading files, many of my friends convert their Chinese names to English or full numbers before downloading them. next I will introduce how to directly download files using Chinese file names without garbled characters. Copy the code as follows...

When downloading files, many of my friends convert their Chinese names to English or full numbers before downloading them. next I will introduce how to directly download files using Chinese file names without garbled characters.

For example

The code is as follows:

$ Filename = "Chinese file name .txt ";
Header ('content-Type: application/octet-stream ');
Header ('content-Disposition: attachment; filename = '. $ filename );
Print "Hello! ";
?>

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.

The output header is actually like this:

The code is as follows:
Content-Disposition: attachment; filename = Chinese file name. txt is a multilingual Content-Disposition encoded according to RFC2231.

It should be defined as follows:

The code is as follows:
Content-Disposition: attachment; filename * = "attachment"

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.
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 will replace the space with +, and replace it with % 20 here.


Example 1

The code is as follows:

$ File = "/tmp/Chinese name .tar.gz ";

$ Filename = basename ($ file );

Header ("Content-type: application/octet-stream ");

// Process the Chinese file name
$ Ua = $ _ SERVER ["HTTP_USER_AGENT"];
$ Encoded_filename = urlencode ($ filename );
$ Encoded_filename = str_replace ("+", "% 20", $ encoded_filename );
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 .'"');
}

Header ('content-Disposition: attachment; filename = "'. $ filename .'"');
Header ("Content-Length:". filesize ($ file ));
Readfile ($ file );
?>

In this way, we have completely solved the problem of garbled Chinese characters.



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.