PHP code to force the download of files (to solve the problem of Chinese file name garbled under IE) _php tutorial

Source: Internet
Author: User
In the middle of a problem is the submission of the Chinese file name directly into the header under IE will become garbled, the solution is to urlencode the file name first and then put in the header, as follows.
Copy CodeThe 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 ');
?>


Solve the PHP header download file in the IE file name garbled there are two common, one is to change the page encoding to UTF8, the other is the Chinese URL into the UrlEncode code can be solved.
Solution One (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. '"');
}


Workaround Two

UrlEncode the file name first and then put it in the header, as follows.
The code is as follows:

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

http://www.bkjia.com/PHPjc/323364.html www.bkjia.com true http://www.bkjia.com/PHPjc/323364.html techarticle in the middle of a problem is the submission of the Chinese file name directly into the header under IE will become garbled, the solution is to urlencode the file name first and then put in the header, as follows. Copy Code ...

  • Related Article

    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.