Recently someone asked me to do the download file method, for the PHP method is as follows: Copy CodeThe code is as follows:
<?php
Header ("Content-type:application/force-download");
Header ("content-disposition:attachment; Filename=ins.jpg ");
ReadFile ("imgs/test_zoom.jpg");
?>
The first line of code is a forced download;
The second line of code is to specify a name for the downloaded content;
The third line of code is to read the downloaded content into the file.
How to troubleshoot garbled files in php download file name 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 CodeThe code is as follows:
<?php
$filename = "Document.txt";
Header (' Content-type:application/octet-stream ');
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:
Copy CodeThe code is as follows:
<?php
$filename = "Chinese filename. txt";
Header (' Content-type:application/octet-stream ');
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 RFC2231 definition, multi-language encoded content-disposition should be so defined:
Copy CodeThe code is 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
Add * before the equals sign after filename
The value of 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 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= "<url encoded UTF-8 file name >"
FF3 attachment; Filename= "UTF-8 filename"
attachment; UTF-8 filename after filename*= "UTF8" <url encoding > "
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:
Copy CodeThe code is as follows:
<?php
$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 ';
?>
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.