The following code submits data and file names in excel format to the server. PHP converts requests to downloadable excel files, in addition, the browser is required to bring up the file download prompt window. a problem occurs in the middle: the submitted Chinese file name is directly put in the header, and it will become garbled in IE. the solution is to urlencode the file name and then put it in the header, as follows.
The code is as follows:
$ 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 ):
The 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:
The code is as follows:
$ 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']);
?>