Php forced file download code (solve the problem of garbled Chinese file names in IE)

Source: Internet
Author: User
Php forced file download code (solve the problem of garbled Chinese file names in IE)

  1. $ File_name = urlencode ($ _ REQUEST ['filename']);
  2. Header ("Pragma: public"); header ("Expires: 0 ");
  3. Header ("Cache-Control: must-revalidate, post-check = 0, pre-check = 0 ");
  4. Header ("Content-Type: application/force-download ");
  5. Header ('content-Type: application/vnd. ms-excel; charset = utf-8 ');
  6. Header ("Content-Transfer-Encoding: binary ");
  7. Header ('content-Disposition: attachment; filename = '. $ file_name );
  8. Echo stripslashes ($ _ REQUEST ['content']);
  9. ?>

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:

  1. $ Filename = "Chinese .txt ";
  2. $ Ua = $ _ SERVER ["HTTP_USER_AGENT"];
  3. $ Encoded_filename = urlencode ($ filename );
  4. $ Encoded_filename = str_replace ("+", "% 20", $ encoded_filename );
  5. Header ('content-Type: application/octet-stream ');
  6. If (preg_match ("/MSIE/", $ ua )){
  7. Header ('content-Disposition: attachment; filename = "'. $ encoded_filename .'"');
  8. } Else if (preg_match ("/Firefox/", $ ua )){
  9. Header ('content-Disposition: attachment; filename * = "utf8'''. $ filename .'"');
  10. } Else {
  11. Header ('content-Disposition: attachment; filename = "'. $ filename .'"');
  12. }

2. urlencode the file name before adding the header. Code:

  1. $ File_name = urlencode ($ _ REQUEST ['filename']);
  2. Header ("Pragma: public"); header ("Expires: 0 ");
  3. Header ("Cache-Control: must-revalidate, post-check = 0, pre-check = 0 ");
  4. Header ("Content-Type: application/force-download"); // add the mandatory download header information
  5. Header ('content-Type: application/vnd. ms-excel; charset = utf-8 ');
  6. Header ("Content-Transfer-Encoding: binary ");
  7. Header ('content-Disposition: attachment; filename = '. $ file_name );
  8. Echo stripslashes ($ _ REQUEST ['content']);
  9. ?>

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.