Home >Backend Development >PHP Tutorial >PHP implementation code for downloading files and solutions to garbled characters in file names

PHP implementation code for downloading files and solutions to garbled characters in file names

高洛峰
高洛峰Original
2016-12-23 12:44:051199browse

Someone recently asked me how to download files. The PHP method 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 to force the download;
The second line of code is to assign a name to the downloaded content;
The third line of code is to download the file. The contents are read into the file.
How to solve garbled characters in PHP download file names
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(&#39;Content-Type: application/octet-stream&#39;); 
header(&#39;Content-Disposition: attachment; filename=&#39; . $filename); 
print "Hello!"; 
?>

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(&#39;Content-Type: application/octet-stream&#39;); 
header(&#39;Content-Disposition: attachment; filename=&#39; . $filename); 
print "Hello!"; 
?>

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, the multi-language encoding Content-Disposition should be defined like this:

Content-Disposition: attachment; filename*="utf8&#39;&#39;%E4%B8%AD%E6%96%87%20%E6%96%87%E4%BB%B6%E5%90%8D.txt"

That is:
After the filename Add * before the equal sign
The value of filename is divided into three segments with single quotes, which are character set (utf8), language (empty) 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 +, which is required here Replace with %20
After testing, it was found that the support of several mainstream browsers is as follows:
IE6 attachment; filename="67d7400f3f906fc09ba7f6ca00e1f3ea"
FF3 attachment; filename="UTF-8 File name"
attachment; filename*="utf8''67d7400f3f906fc09ba7f6ca00e1f3ea"
O9 attachment; filename="UTF-8 file name"
Safari3(Win) doesn’t seem to support it? None of the above methods will work
It seems that the program must be written like this to support all mainstream browsers:

<?php 
$ua = $_SERVER["HTTP_USER_AGENT"]; 
$filename = "中文 文件名.txt"; 
$encoded_filename = urlencode($filename); 
$encoded_filename = str_replace("+", "%20", $encoded_filename); 
header(&#39;Content-Type: application/octet-stream&#39;); 
if (preg_match("/MSIE/", $ua)) { 
header(&#39;Content-Disposition: attachment; filename="&#39; . $encoded_filename . &#39;"&#39;); 
} else if (preg_match("/Firefox/", $ua)) { 
header(&#39;Content-Disposition: attachment; filename*="utf8\&#39;\&#39;&#39; . $filename . &#39;"&#39;); 
} else { 
header(&#39;Content-Disposition: attachment; filename="&#39; . $filename . &#39;"&#39;); 
} 
print &#39;ABC&#39;; 
?>

For more PHP implementation code for downloading files and solutions to garbled characters in file names, please pay attention to the PHP Chinese website for related articles!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Rookie's PHP Notes (4)Next article:Rookie's PHP Notes (4)