By setting the Content-Type to application/octet-stream, you can download dynamically generated content as a file. I believe everyone knows this. Then use Content-Disposition to set the downloaded file name. Many people know this. Basically, the download program is written like this:
<?php $filename = "document.txt";header('Content-Type: application/octet-stream'); Copy after login header('Content-Disposition: attachment; filename=' . $filename);print "Hello!";?> Copy after login |
After opening it with a browser, you can download document.txt.
However, if $filename is UTF-8 encoded, some browsers cannot handle it properly. For example, slightly change the above program:
<?php $filename = "中文 文件名.txt";header('Content-Type: application/octet-stream'); Copy after login header('Content-Disposition: attachment; filename=' . $filename);print "Hello!";?> Copy after login |
Save the program in UTF-8 encoding and then access it, the file name downloaded by IE6 will be garbled. The file name downloaded under FF3 only has the word "Chinese". Everything works fine under Opera 9.
The output header actually looks like this:
Content-Disposition: attachment; filename=Chinese file name.txt
In fact, according to the definition of RFC2231, multi-language encoding Content-Disposition It should be defined like this:
Content-Disposition: attachment; filename*="utf8''%E4%B8%AD%E6%96%87%20%E6%96%87%E4%BB %B6%E5%90%8D.txt"
That is:
Before the equal sign after filename, add *. The value of filename is divided into three sections with single quotes, which are characters. Set (utf8), language (null) and urlencoded file name. It is best to add double quotes, otherwise the part after the space in the file name will not be displayed in Firefox. Note that the result of urlencode is not the same as the result of php's urlencode function. PHP's urlencode will replace the space with +, and here it needs to be replaced with %20
After testing, it was found that the support of 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"
Safari3(Win) seems not supported? None of the above methods will work
It seems that the program must be written like this to support all major browsers:
<?php $ua = $_SERVER["HTTP_USER_AGENT"];$filename = "中文 文件名.txt"; Copy after login $encoded_filename = urlencode($filename);$encoded_filename = str_replace("+", "%20", Copy after login $encoded_filename);header('Content-Type: application/octet-stream'); Copy after login if (preg_match("/MSIE/", $ua)) { header('Content-Disposition: attachment; Copy after login filename="' . $encoded_filename . '"');} else if (preg_match("/Firefox/", $ua)) Copy after login { header('Content-Disposition: attachment; filename*="utf8''' . $filename . '"');} Copy after login else { header('Content-Disposition: attachment; filename="' . $filename . '"'); Copy after login }print 'ABC';?> Copy after login Copy after login |