PHP download file name garbled problem detailed

Source: Internet
Author: User
Tags php download urlencode

By setting the Content-type to Application/octet-stream, you can download the dynamically generated content as a file and believe that everyone will. Then use Content-disposition to set the download file name, this also has a lot of people know. Basically, the download program is written like this:

header('Content-Disposition: attachment; filename=' . $filename);print  "Hello!";?>

This allows you to download document.txt after you open it in a browser.

However, if the $filename is UTF-8 encoded, some browsers will not work properly. For example, to change the above program slightly:

header('Content-Disposition: attachment; filename=' . $filename);print  "Hello!";?>

Save the program to UTF-8 code and then access, IE6 download the file name will be garbled. FF3 download under the file name is only "Chinese" two words. Opera 9 under all normal.

The header of the output actually looks like this:

Content-disposition:attachment; Filename= Chinese file name. txt

In fact, according to the definition of RFC2231, the content-disposition of multi-language encoding should be defined as follows:

Content-disposition:attachment; filename*= "UTF8 '%e4%b8%ad%e6%96%87%20%e6%96%87%e4%bb% b6%e5%90%8d.txt"

That

The value of * filename before the equals sign in filename is divided into three paragraphs in single quotes, which are character sets (UTF8), language (empty), and UrlEncode file names. It is best to add double quotes, otherwise the file name hollow lattice behind the part in Firefox does not appear to notice the results of UrlEncode and PHP UrlEncode function results are not the same, PHP UrlEncode will replace the space into +, and here needs to be replaced by%20

After testing, it was found that several major browsers support the following:

IE6 attachment; Filename= ""

FF3 attachment; Filename= "UTF-8 filename"

attachment; filename*= "UTF8 '"

O9 attachment; Filename= "UTF-8 filename"

Safari3 (Win) seemingly does not support? None of the above methods.

In this way, the program has to write to support all major browsers:

$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 . '"');

}print 'ABC';?>

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.