One problem encountered in the middle is that the submitted Chinese file name is directly put in the header and will become garbled characters in IE. The solution is to urlencode the file name first and then put it in the header, as shown below.
Copy codeThe Code is as follows:
<? Php
$ File_name = urlencode ($ _ REQUEST ['filename']);
Header ("Pragma: public"); header ("Expires: 0 ");
Header ("Cache-Control: must-revalidate, post-check = 0, pre-check = 0 ");
Header ("Content-Type: application/force-download ");
Header ('content-Type: application/vnd. ms-excel; charset = UTF-8 ');
Header ("Content-Transfer-Encoding: binary ");
Header ('content-Disposition: attachment; filename = '. $ file_name );
Echo stripslashes ($ _ REQUEST ['content']);
?>
There are two common methods to solve PHP Header file garbled characters in IE file names. One is to change the page encoding to utf8, the other is to enter the urlencode encoding for the Chinese url.
Solution 1 (my page is UTF-8 encoded ):
Copy codeThe Code is as follows:
$ Filename = "Chinese .txt ";
$ Ua = $ _ SERVER ["HTTP_USER_AGENT"];
$ 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 .'"');
}
Solution 2
Urlencode the file name before adding the header, as shown below.
The Code is as follows:
Copy codeThe Code is as follows: <? Php
$ File_name = urlencode ($ _ REQUEST ['filename']);
Header ("Pragma: public"); header ("Expires: 0 ");
Header ("Cache-Control: must-revalidate, post-check = 0, pre-check = 0 ");
Header ("Content-Type: application/force-download ");
Header ('content-Type: application/vnd. ms-excel; charset = UTF-8 ');
Header ("Content-Transfer-Encoding: binary ");
Header ('content-Disposition: attachment; filename = '. $ file_name );
Echo stripslashes ($ _ REQUEST ['content']);
?>