Solutions to garbled file names generated by php: 1. Replace all numbers with " "; 2. Add "header("Content-type: application/vnd.ms-excel");"; 3. , Delete the spaces on both sides of "filename=xxx".
The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer
Solution to garbled PHP export file name
Problem scenario:
Statistical data needs to be added with a everywhere excel function, so I used composer to install the phpspreadsheet component, process the data and export it. When encountering ASCII named file names, everything is exported normally, but when encountering Chinese file names, the exported file names are garbled.
The solution is posted directly here:
1. File name processing
Because PHP’s urlencode function will Various whitespace characters are transcoded into numbers. This number will not be escaped in either Chrome or Firefox.
, so here all the numbers are replaced again with
$file_name = '导出文件.xls'; // 这是原本要导出的文件名 $encoded_filename = url_encode($file_name);// 将文件名进行urlencode转码 $encoded_filename = str_replace('+', '%20', $encoded_filename);
2 .Header file: File type declaration
If the export is a file and it is not a certain excel type, use file header No.1
I exported an xls under excel Format table, so I am using file header No.2
// 文件头No.1: 告诉浏览器这里要有一个文件流输出 header("Content-type: application/octet-stream"); // 文件头No.2: 告诉浏览器这里要输出一个excel文件 header("Content-type: application/vnd.ms-excel");
Header file: Download file name declaration
This step is key, there are two requirements Things to note
Whether it is filename=xxx or filename*=xxx, there should be no spaces on both sides of the equal sign
filename*=xxx. This configuration item actually consists of 3 parts, namely Character set (utf8), language (empty) and urlencoded file name, where language is an empty string, enclosed in single quotes, and there should be no spaces between the three parts.
header('Content-Disposition: attachment; filename="foo-%c3%a4.xls"; filename*=UTF-8\'\'foo-%c3%a4.xls');
I used the above code to test chrome, firefox, 360, and ie11 and all worked well. Hope it helps you too.
[Recommended learning: PHP video tutorial]
The above is the detailed content of How to solve the problem of garbled file names generated by php. For more information, please follow other related articles on the PHP Chinese website!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


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

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

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

SublimeText3 English version
Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.