Php forced file download code (solve the problem of garbled Chinese file names in IE)
- $ 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']);
- ?>
Solve PHP Header download file Chinese garbled characters in IE File name: one is to change the page encoding to utf8. The other is url encoding for Chinese URLs. The following describes the specific implementation code of the solution. 1. the page is UTF-8 encoded:
- $ 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 .'"');
- }
2. urlencode the file name before adding the header. Code:
- $ 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"); // add the mandatory download header information
- 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']);
- ?>
|