Parsing how to solve garbled problems in PHP download filename _php tips

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:
Copy Code code as follows:

$filename = "Document.txt";
Header (' Content-type:application/octet-stream ');
Header (' Content-disposition:attachment filename= '. $filename);
print "hello!";
?>

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

$filename = "Chinese filename. txt";
Header (' Content-type:application/octet-stream ');
Header (' Content-disposition:attachment filename= '. $filename);
print "hello!";
?>

the header of the output actually looks like this:
Content-disposition:attachment; Filename= Chinese filename txt In fact, according to the definition of RFC2231, multi-language encoded content-disposition should be so defined:
Content-disposition:attachment; filename*= "UTF8 '%e4%b8%ad%e6%96%87%20%e6%96%87%e4%bb%b6%e5%90%8d.txt" namely:
Add * Before the equals sign in filename
filename values are 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 part of the filename will not appear in Firefox
• Note that the results of UrlEncode and PHP UrlEncode function results are not the same, PHP UrlEncode will replace the space into a +, and here need to substitute for%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)
doesn't seem to support it? None of the above methods.
In this way, the program has to write to support all major browsers:
Copy Code code as follows:

$ua = $_server["Http_user_agent"];
$filename = "Chinese filename. txt";
$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.