PHP file download Chinese file name garbled
This article mainly introduces the garbled Chinese file name of PHP file download. Interested friends can refer to it. I hope it will be helpful to everyone.
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 the header in, and then the problem is solved.
$filename = urlencode("下载文档"); header ( "Content-disposition: attachment; filename=$filename.xls" );
It is said online that in the definition of RFC2231, the Content-Disposition of multi-language encoding should be defined like this:
The code is as follows:
Content-Disposition: attachment; filename*="utf8''%E6%B5%8B%E8%AF%95.html"
That is:
The equal sign after filename must be preceded by *
The value of filename is divided into three segments with single quotes, which are character set (utf8), language (empty) and urlencoded file name.
So at this time, the file name should be converted to URL encoding. 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 . '"'); }
Summary: The above is the entire content of this article, I hope it can It will be helpful to everyone’s study.
Related recommendations:
Detailed explanation of the principles and examples of chain operation in PHP
How to use PHP to access Alipay’s instant payment function
php method to realize the manipulation of mysqli database
The above is the detailed content of PHP file download Chinese file name garbled. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











In SpringBoot, use Redis to cache OAuth2Authorization object. In SpringBoot application, use SpringSecurityOAuth2AuthorizationServer...

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

In IntelliJ...

In processing next-auth generated JWT...

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.

How to solve the problem of printing spaces in IDEA console logs? When using IDEA for development, many developers may encounter a problem: the console printed...

In MySQL, add fields using ALTERTABLEtable_nameADDCOLUMNnew_columnVARCHAR(255)AFTERexisting_column, delete fields using ALTERTABLEtable_nameDROPCOLUMNcolumn_to_drop. When adding fields, you need to specify a location to optimize query performance and data structure; before deleting fields, you need to confirm that the operation is irreversible; modifying table structure using online DDL, backup data, test environment, and low-load time periods is performance optimization and best practice.
