PHP attachment Download Chinese name garbled solution method

Source: Internet
Author: User
Tags php programming urlencode
This article describes the PHP attachment download Chinese name garbled solution. Share to everyone for your reference, as follows:

In PHP, if the file name you want to download is in Chinese, the file header will appear garbled.

At this point, you need to encode the title, that is, advanced UrlEncode, and then put in the header, and then solve the problem.

$filename = UrlEncode ("Download Document"); header ("Content-disposition:attachment; Filename= $filename. xls ");

Online, in the definition of RFC2231, the multi-language encoding of content-disposition should be defined as:

Copy the Code code as follows:

Content-disposition:attachment; filename*= "UTF8"%e6%b5%8b%e8%af%95.html "


That

Add * before the equals sign after filename
The value of filename is divided into three segments with single quotation marks, namely the character set (UTF8), language (empty), and UrlEncode file names.

So this should be a URL encoding file name conversion, the use of PHP UrlEncode is easy to take care of

$ua = _server["Http_user_agent"]; $filename = "Chinese file name: txt"; $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. '"');}

I hope this article is helpful to you in PHP programming.

The above describes the PHP attachment download Chinese name garbled solution, including the content of the URL encoding, I hope the PHP tutorial interested in a friend helpful.

  • 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.