Home > Backend Development > PHP Tutorial > Solution to the garbled Chinese name of PHP attachment download

Solution to the garbled Chinese name of PHP attachment download

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-29 09:10:10
Original
852 people have browsed it

The example in this article describes the solution to the garbled Chinese name of PHP attachment download. Share it with everyone for your reference, the details are as follows:

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

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

$filename = urlencode("下载文档");
header ( "Content-disposition: attachment; filename=$filename.xls" );

Copy after login

It is said online that in the definition of RFC2231, the Content-Disposition of multi-language encoding should be defined like this:

Copy the code The code is as follows:

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


That is:

Before the equal sign after filename, add *
The value of filename is divided into three sections with single quotes, which are character sets (utf8) , language (empty) and urlencoded file name.

So at this time, the file name should be url encodedconverted. It can be easily done using php's urlencode

$ua = _SERVER["HTTP_USER_AGENT"];
$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 . '"');
}

Copy after login

I hope this article will be helpful to everyone in PHP programming.

The above introduces the solution to the garbled Chinese name of PHP attachment download, including the URL encoding. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template