Home Backend Development PHP Tutorial How to solve garbled characters in PHP download file names_PHP Tutorial

How to solve garbled characters in PHP download file names_PHP Tutorial

Jul 13, 2016 pm 05:42 PM
application o php download for Garbled characters Bundle file name solve set up pass

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 />?>
Copy after login

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 />?>
Copy after login

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
Copy after login

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"
Copy after login

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>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486039.htmlTechArticleBy 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...
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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

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

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

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

Top 10 global digital currency trading apps recommended (2025 currency trading software ranking) Top 10 global digital currency trading apps recommended (2025 currency trading software ranking) Mar 12, 2025 pm 05:48 PM

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.

How to install and register the btc trading app? How to install and register the btc trading app? Feb 21, 2025 pm 07:09 PM

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.

Which digital currency app trading software is the best? Digital currency trading software big spot Which digital currency app trading software is the best? Digital currency trading software big spot Mar 07, 2025 pm 06:45 PM

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.

Top 10 PHP CMS Platforms For Developers in 2024 Top 10 PHP CMS Platforms For Developers in 2024 Dec 05, 2024 am 10:29 AM

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

See all articles