PHP File Download (and the downloaded file name is garbled in Chinese)

Source: Internet
Author: User

 <? Php <br/> // clear the cache <br/> header ("Pragma: No-Cache "); <br/> // set the expiration time <br/> header ("expires: 0"); <br/> header ("cache-component: Must-revalidate, post-check = 0, pre-check = 0 "); <br/> // set the downloaded character set <br/> header (" Content-Type: application/octet-stream; charset = UTF-8 "); <br/>/** <br/> * due to the file name UTF-8 encoding, coupled with the differences between different browsers, therefore, the downloaded file name may be garbled. <br/> ***/<br/> // check the browser used by visitors on the page. <br/> $ UA = $ _ server ["http_user_agent"]; <br/> // URL encoding <br/> $ filename = $ this-> file_name. ". doc "; <br/> $ encoded_filename = urlencode ($ filename); <br/> $ encoded_filename = str_replace (" + "," % 20 ", $ encoded_filename ); <br/>/** <br/> * Before the equal sign after filename, add * <br/> the value of filename is divided into three sections by single quotation marks, which are character sets (utf8), language (null), and urlencode. <Br/> it is best to add double quotation marks, otherwise the section after the space in the file name cannot be displayed in Firefox <br/> note that the urlencode result is different from the urlencode function result in PHP, in PHP, urlencode will replace the space with +, and replace it with % 20 <br/> after experiment, we found that the support for several mainstream browsers is as follows: <br/> IE6 attachment; filename = "<URL encoded UTF-8 File Name>" <br/> ff3 attachment; filename = "UTF-8 file name" <br/> attachment; filename * = "utf8'' <URL-encoded UTF-8 File Name>" <br/> O9 attachment; filename = "UTF-8 file name" <br/> ***/<br/> If (preg_match ("/MSIE/", $ UA )) {<br/> header ('conten T-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/> header (" content-transfer-encoding: Binary "); <br/> readfile (getcwd (). $ this-> file_path); <br/>?>

 

I. Causes of Chinese garbled characters

There are two reasons for garbled characters: charset)
The setting is incorrect, causing the browser to parse by wrong encoding, resulting in "tianshu", which is full of screens. The second is that the file is opened with wrong encoding, and then saved, for example, a text file is
Gb2312 encoding, but in the UTF-8 encoding to open and save. To solve the above garbled code problem, you must first know which stages of development involve encoding:

1. file encoding

2. Page declarative Encoding



Ii. Common Errors and solutions:

 

1. The database uses utf8 encoding, while the page declarative encoding is gb2312.
This is the most common cause of Garbled text. In this case, the Select data in the PHP script is garbled. You need to use mysql_query ("Set
Names GBK "); To set the MySQL connection encoding, ensure that the page declarative encoding is consistent with the connection encoding set here (GBK is an extension of gb2312
). If the page is UTF-8 encoded, you can use: mysql_query ("set names utf8 ");
Note that it is utf8 instead of a general UTF-8. If the encoding stated on the page is consistent with the internal encoding of the database, no connection encoding can be set.
Note: In fact, MySQL data input and output are more complex than described above. MySQL configuration file my. ini defines two default encodings, which are in [client]
Default-character-set and default-character-set in [mysqld]
To set the encoding used by the client connection and the database respectively by default. The code we specified above is actually the command line parameter when the mysql client connects to the server
Character_set_client is used to tell the MySQL server what encoding the client data is received, rather than the default encoding.

 


2. The page declarative encoding is inconsistent with the file encoding. this rarely happens, because if the encoding is inconsistent, the attacker will see garbled code in the browser when making the page. It is more often to make minor changes after release.
The error code is used to open the page and save the error code. Or you can use some FTP software to directly modify files online, such as CuteFTP. The conversion error is caused by incorrect software encoding.



3. Some friends who rent a VM clearly confirm that the above three codes are correctly set and there are still garbled characters. For example, the web page is gb2312 encoding, ie and other browsers open but always recognized as a UTF-8
The header of the web page already states that it is gb2312. manually modify the browser code to gb2312
The page is displayed normally. The cause is that Apache sets the global default encoding of the server and adds adddefacharcharset to httpd. conf.
UTF-8
. At this time, the server will first send an HTTP header to the browser, which has a higher priority than the declarative encoding in the page, and the natural browser will recognize the error. There are two solutions. Ask the Administrator to add them to the virtual machine in the configuration file.
Adddefacharcharset gb2312 overwrites the global configuration, or you can configure it in. htaccess in your own directory.


Conclusion: To solve PHP Chinese garbled characters in a single sentence, the encoding format must be consistent.


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.