PHP to download files to achieve the code and file name in the mess code solution

  • 2020-03-31 21:31:57
  • OfStack

Recently, someone asked me how to download files. For PHP, the 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 a mandatory download;
The second line of code gives the download a name;
The third line of code reads the download into the file.
How to solve the garble in PHP download file name
By setting the content-type to application/octet-stream, you can download dynamically generated Content as a file, as you can. The file name of the download is set with content-disposition, as many of you know. Basically, downloads are written like this:
 
<?php 
$filename = "document.txt"; 
header('Content-Type: application/octet-stream'); 
header('Content-Disposition: attachment; filename=' . $filename); 
print "Hello!"; 
?> 

After you open it with a browser, you can download document.txt.
However, if $filename is encoded in utf-8, some browsers cannot handle it properly. For example, change the above procedure slightly:
 
<?php 
$filename = " Chinese   The file name .txt"; 
header('Content-Type: application/octet-stream'); 
header('Content-Disposition: attachment; filename=' . $filename); 
print "Hello!"; 
?> 

If you save the program as utf-8 code and then access it, the file names downloaded by IE6 will be scrambled. FF3 download file name only "Chinese" two words. Everything is fine in Opera 9.
The output header actually looks like this:
The Content - Disposition: attachment; Filename = Chinese filename. TXT as defined by RFC2231, the multi-language encoding of content-disposition 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 is:
* is added to the equal sign after filename
The value of filename is divided into three segments with single quotes, which are the character set (utf8), the language (empty), and the urlencode filename.
It's best to put double quotes, or the space after the file name won't show up in Firefox
Note that the urlencode result is not quite the same as the urlencode function in PHP, which replaces the space with a +, but in this case with %20
Through the experiment, we found that the support of several major browsers is as follows:
IE6 attachment; Filename = "< Utf-8 file name after URL encoding >"
FF3 attachment; Filename =" utf-8 filename"
Attachment; Filename * = "utf8 '< Utf-8 file name after URL encoding >"
O9 attachment; Filename =" utf-8 filename"
Safari3(Win) does not seem to be supported? None of the above works
In this case, the program must be written like this to support all major browsers:
 
<?php 
$ua = $_SERVER["HTTP_USER_AGENT"]; 
$filename = " Chinese   The file name .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'; 
?> 

Related articles: