Php forcibly downloads the file code (solves the problem of garbled Chinese file names in IE)

Source: Internet
Author: User

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']);
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.