PHP file name garbled

Source: Internet
Author: User

By setting Content-Type to application/octet-stream, you can download the dynamically generated Content as an object. You can use Content-Disposition to set the downloaded file name. Basically, download programs are written like this:

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

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

However, if $ filename is UTF-8 encoded, Some browsers won't be able to handle it. For example, change the above program slightly:

 
     
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:

Content-Disposition: attachment; filename = Chinese file name .txt
In fact, according to RFC2231, the Content-Disposition of multi-language encoding should be defined as follows:

Content-Disposition: attachment; filename * = "attachment"

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 section after the space in the file name is not 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 +, replace the value with % 20.

After experiments, we found that the support for several mainstream browsers is as follows:

IE6 attachment; filename =" "

FF3 attachment; filename = "UTF-8 file name"

Attachment; filename * = "utf8'' "

O9 attachment; filename = "UTF-8 file name"

Does Safari3 (Win) seem to be unsupported? None of the above methods works.

In this case, the program must be written in this way to support all mainstream 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.