Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

PHP attachment download Chinese name garbled what to do

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to download Chinese name garbled in PHP attachment. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

The details are as follows:

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

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

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

According to the Internet, in the definition of RFC2231, a multilingual coded Content-Disposition should be defined as follows:

Content-Disposition: attachment; filename*= "utf8''%E6%B5%8B%E8%AF%95.html"

That is:

Add * before the equal sign after filename.

The value of filename is divided into three segments in single quotation marks, namely, the character set (utf8), the language (empty), and the file name passed by urlencode.

So at this time, the file name should be converted by url, and it can be easily done by using php's urlencode.

$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. This is the end of the article on "how to download Chinese name garbled in PHP attachment". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report