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';?>