PHP attachment Download Chinese name garbled solution _php Tips

Source: Internet
Author: User
Tags urlencode

This article describes the PHP attachment download Chinese name garbled solution. Share to everyone for your reference, specific as follows:

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

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

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

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

Copy 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 paragraphs in single quotes, which are character sets (UTF8), language (empty), and UrlEncode file names.

So at this time should be a URL code conversion of the file name, using PHP UrlEncode is easy to get it done

$ua = _server["Http_user_agent"];
$filename = "Chinese filename. 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 will help you with the PHP program design.

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.