PHP attachments download Chinese names garbled solution

Source: Internet
Author: User
This article mainly introduces the solution of garbled Chinese names for PHP attachment downloading, analyzes the causes of garbled Chinese characters and the corresponding encoding conversion methods, which has some reference value, for more information about how to download PHP attachments, see the example below. We will share this with you for your reference. The details are as follows:

In PHP, if the file name to be downloaded is called Chinese, the file title is garbled.

In this case, you need to encode the title, that is, urlencode, and then put the header. then the problem is solved.

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

According to the Internet, in RFC2231 definition, the Content-Disposition of multi-language encoding should be defined as follows:

The code is as follows:

Content-Disposition: attachment; filename * = "utf8'%e6%b5%8b%e8%af%95.html"


That is:

Add the equal sign after filename *
The filename value is divided into three sections by single quotes, which are character set (utf8), Language (null), and URL code file names.

Therefore, url encoding should be performed on the file name. using php's urlencode can be easily completed.

$ 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 will help you with PHP programming.

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.