Home Backend Development PHP Tutorial PHP download file, Chinese garbled problem

PHP download file, Chinese garbled problem

Nov 22, 2016 pm 04:44 PM

By setting the Content-Type to application/octet-stream, you can download dynamically generated content as a file. I believe everyone knows this. Then use Content-Disposition to set the downloaded file name. Many people know this. Basically, the download program is written like this:

<?php
$filename = "document.txt";
header('Content-Type: application/octet-stream');
header('Content-Disposition : attachment; filename=' . $filename);
print "Hello!";
?>
[/cdoe]
After opening it with a browser, you can download document.txt.

However, if $filename is UTF-8 encoded, some browsers cannot handle it properly. For example, slightly change the above program:
[code]
<?php
$filename = "Chinese file name.txt";
header('Content-Type: application/octet-stream');
header(' Content-Disposition: attachment; filename=' . $filename);
print "Hello!";
?>
[/cdoe]
Save the program in UTF-8 encoding and access it again. The file name downloaded by IE6 will be garbled. . The file name downloaded under FF3 only has the word "Chinese". Everything works fine under Opera 9.

The output header actually looks like this:

Content-Disposition: attachment; filename=Chinese file name.txt In fact, according to the definition of RFC2231, the multi-language encoding Content-Disposition should be defined like this:

Content-Disposition: attachment ; filename*="utf8''%E4%B8%AD%E6%96%87%20%E6%96%87%E4%BB%B6%E5%90%8D.txt" that is:

after filename Add * before the equal sign
The value of filename is divided into three segments with single quotes, which are character set (utf8), language (empty) and urlencoded file name.
It is best to add double quotes, otherwise the part after the space in the file name will not be displayed in Firefox
Note that the result of urlencode is not the same as the result of php's urlencode function. PHP's urlencode will replace the space with +, which is required here Replace with %20
After testing, it was found that the support of several mainstream browsers is as follows:

IE6 attachment; filename="<UTF-8 file name after URL encoding>"
FF3 attachment; filename="UTF- 8 file name"
attachment; filename*="utf8''<UTF-8 file name after URL encoding>"
O9 attachment; filename="UTF-8 file name"
Safari3(Win) Doesn’t seem to support it? None of the above methods work

It seems that the program must be written like this to support all mainstream browsers:
[code]
<?php
$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 . '"');
}
print 'ABC';
?>

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles