


How to solve garbled characters in PHP download file names_PHP Tutorial
By setting Content-Type to application/octet-stream, dynamically generated content can be downloaded 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<br />$filename = "document.txt";<br />header(Content-Type: application/octet-stream);<br />header(Content-Disposition: attachment; filename= . $filename);<br /><br />print "Hello!";<br />?>
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:
<?php<br />$filename = "中文 文件名.txt";<br />header(Content-Type: application/octet-stream);<br />header(Content-Disposition: attachment; filename= . $filename);<br /><br />print "Hello!";<br />?>
If you save the program in UTF-8 encoding and then 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=中文 文件名.txt
In fact, according to the definition of RFC2231, the Content-Disposition of multi-language encoding 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"
i.e.:
- Before the equal sign after filename, add *
- 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 spaces with +, and here it needs to be replaced with %20
After testing, it was found that the support of several mainstream browsers is as follows:
IE6 | attachment; filename=" |
FF3 | attachment; filename="UTF-8文件名" |
attachment; filename*="utf8 |
|
O9 | attachment; filename="UTF-8文件名" |
Safari3(Win) | 貌似不支持?上述方法都不行 |
It seems that the program must be written like this to support all major browsers:
<?php<br /><br />$ua = $_SERVER["HTTP_USER_AGENT"];<br /><br />$filename = "中文 文件名.txt";<br />$encoded_filename = urlencode($filename);<br />$encoded_filename = str_replace("+", "%20", $encoded_filename);<br /><br />header(Content-Type: application/octet-stream);<br /><br />if (preg_match("/MSIE/", $ua)) {<br /> header(Content-Disposition: attachment; filename=" . $encoded_filename . ");<br />} else if (preg_match("/Firefox/", $ua)) {<br /> header(Content-Disposition: attachment; filename*="utf8\ . $filename . ");<br />} else {<br /> header(Content-Disposition: attachment; filename=" . $filename . ");<br />}<br /><br />print ABC;<br />?><br>

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

AI Hentai Generator
Generate AI Hentai for free.

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

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This article recommends the top ten digital currency trading apps in the world, including Binance, OKX, Huobi Global, Coinbase, Kraken, Gate.io, KuCoin, Bitfinex, Gemini and Bitstamp. These platforms have their own characteristics in terms of transaction pair quantity, transaction speed, security, compliance, user experience, etc. For example, Binance is known for its high transaction speed and extensive services, while Coinbase is more suitable for novices. Choosing a platform that suits you requires comprehensive consideration of your own needs and risk tolerance. Learn about the world's mainstream digital currency trading platforms to help you conduct digital asset trading safely and efficiently.

This article will provide a detailed introduction to how to install and register a Bitcoin trading application. The Bitcoin trading app allows users to manage and trade cryptocurrencies such as Bitcoin. The article guides users through the installation and registration process step by step, including downloading applications, creating accounts, performing identity verification, and first deposit. The goal of the article is to provide beginners with clear and easy-to-understand guidelines to help them easily enter the world of Bitcoin trading.

There is no single "best" digital currency trading app, and the choice depends on personal needs. 1. OKX has powerful functions and rich currency types; 2. Binance has high liquidity and diverse transaction types; 3. Gate.io provides unique functions such as staking mining; 4. Huobi Global has a friendly interface and multi-language support; 5. Kraken focuses on security; 6. Coinbase is suitable for beginners and focuses on user education. When choosing, you need to consider security, liquidity, handling fees, functions, user experience and other factors.

CMS stands for Content Management System. It is a software application or platform that enables users to create, manage, and modify digital content without requiring advanced technical knowledge. CMS allows users to easily create and organize content
