PHP to do download file implementation code and file name garbled solution _php Tutorial

Source: Internet
Author: User
Tags php download
Recently someone asked me to do the download file method, for the PHP method is as follows:
Copy CodeThe code 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 forced to download;
The second line of code is to specify a name for the downloaded content;
The third line of code is to read the downloaded content into the file.
How to resolve garbled characters in the PHP download file name
By setting the Content-type to Application/octet-stream, the dynamically generated content can be downloaded as a file, which we believe will be. Then use the Content-disposition settings to download the file name, this also has a lot of people know it. Basically, the download program is written like this:
Copy CodeThe code is as follows:
$filename = "Document.txt";
Header (' Content-type:application/octet-stream ');
Header (' content-disposition:attachment; Filename= '. $filename);
print "hello!";
?>

This allows you to download document.txt after you open it in a browser.
However, if $filename is UTF-8 encoded, some browsers will not be able to handle it properly. For example, the above program to change a little bit:
Copy CodeThe code is as follows:
$filename = "Chinese filename. txt";
Header (' Content-type:application/octet-stream ');
Header (' content-disposition:attachment; Filename= '. $filename);
print "hello!";
?>

Save the program to UTF-8 encoding and then access, IE6 downloaded file name will be garbled. FF3 Download the file name is only "Chinese" two characters. Opera 9 under all normal.
The header of the output is actually like this:
Content-disposition:attachment; Filename=. txt in fact, according to RFC2231 definition, multi-language encoding content-disposition should be defined as:
Copy CodeThe code is as follows:
Content-disposition:attachment; filename*= "UTF8"%e4%b8%ad%e6%96%87%20%e6%96%87%e4%bb%b6%e5%90%8d.txt "

That
Add * before the equals sign after filename
The value of filename is divided into three segments with single quotation marks, namely the character set (UTF8), language (empty), and UrlEncode file names.
It is best to add double quotes, otherwise the file name behind the hollow lattice is not displayed in Firefox
Note that the results of UrlEncode and PHP UrlEncode function results are not the same, PHP UrlEncode will replace the space with A +, and here need to change to%20
After testing, several mainstream browsers have been found to support the following scenarios:
IE6 attachment; Filename= " "
FF3 attachment; Filename= "UTF-8 file name"
Attachment filename*= "UTF8" "
O9 attachment; Filename= "UTF-8 file name"
Safari3 (Win) does not seem to support? None of the above methods.
In this way, the program has to be written so that it can support all major browsers:
Copy CodeThe code is as follows:
$ua = $_server["Http_user_agent"];
$filename = "Chinese 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. '"');
}
print ' ABC ';
?>

http://www.bkjia.com/PHPjc/322984.html www.bkjia.com true http://www.bkjia.com/PHPjc/322984.html techarticle recently someone asked me to do the download file method, for the PHP method is as follows: Copy the code as follows: PHP header ("Content-type:application/force-download"); Header (" Content-dispositio ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.