


PHP implementation code for downloading files and solutions to garbled characters in file names_PHP tutorial
Someone recently asked me how to download files. The php method is as follows:
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=ins.jpg");
readfile( "imgs/test_Zoom.jpg");
?>
The first line of code is to force download;
The second line of code is to assign a name to the downloaded content;
The third line of code reads the downloaded content into the file.
How to solve garbled characters in PHP download file names
By setting the Content-Type to application/octet-stream, you can download dynamically generated content as a file. I believe everyone will know this. . Then use Content-Disposition to set the downloaded file name. Many people know this. Basically, the download program is written like this:
$filename = "document.txt";
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $filename);
print "Hello!";
?>
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:
$filename = "Chinese file name.txt";
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $filename);
print "Hello!";
?>
If you 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:
Before the equal sign after filename, add * 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 spaces 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 spaces with + , and 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 file name"
attachment; filename*="utf8''
O9 attachment; filename="UTF- 8File name"
Safari3(Win) doesn’t seem to support it? None of the above methods will work
It seems that the program must be written like this to support all mainstream browsers:
$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';
?>

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











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
