PHP Chinese garbled Solution summary Analysis _php Skills

Source: Internet
Author: User
Tags file upload mysql client mysql code mysql in php script readfile
A The first is the coding of PHP pages
1. php file itself and the encoding of the Web page should match
A. If you want to use gb2312 encoding, then PHP to output headers: header ("content-type:text/html; Charset=gb2312″), static page add <meta http-equiv= "Content-type" content= "text/html"; Charset=gb2312″&gt, all files are encoded in ANSI, can be opened with Notepad, save as a selection encoding for ANSI, overwriting the source file.
B. If you want to use UTF-8 encoding, then PHP to output headers: header ("content-type:text/html; Charset=utf-8″), static page add <meta http-equiv= "Content-type" content= "text/html"; CHARSET=UTF-8″&GT, the encoding format for all files is utf-8. Save As Utf-8 may be a bit troublesome, general Utf-8 file at the beginning will have a BOM, if use session will be a problem, can be used to save EditPlus, in EditPlus, tool-> parameter selection-> file->utf-8 signature, Select Always Delete, then save the BOM information can be removed.
2. PHP itself is not Unicode, all functions such as substr have to be changed to MB_SUBSTR (need to install mbstring extension), or iconv transcoding.
two. PHP's data interaction with MySQL
PHP and database coding should be consistent
1. Modify the MySQL configuration file My.ini or My.cnf,mysql best use UTF8 encoding
[MySQL]
Default-character-set=utf8
[Mysqld]
Default-character-set=utf8
Default-storage-engine=myisam
By adding under [Mysqld]:
Default-collation=utf8_bin
init_connect= ' SET NAMES utf8′
2. In the need to do the database operation of the PHP program before adding mysql_query ("Set Names ' code ')", Encoding and PHP code consistent, if the PHP code is gb2312 that MySQL code is gb2312, If it is utf-8 that MySQL code is UTF8, so when inserting or retrieving data will not appear garbled
three. PHP is related to the operating system
Windows and Linux are not encoded in the same way, in Windows environment, when invoking PHP functions when the parameters are Utf-8 encoding error, such as Move_uploaded_file (), FileSize (), ReadFile (), etc. These functions are often used when processing uploads and downloads, and the following errors may appear when invoked:
Warning:move_uploaded_file () [function.move-uploaded-file]:failed to open stream:invalid argument.
Warning:move_uploaded_file () [Function.move-uploaded-file]:unable to move ' to ' in ...
Warning:filesize () [Function.filesize]: Stat failed for ... in ...
Warning:readfile () [Function.readfile]: Failed to open stream:invalid argument in..
In the Linux environment with GB2312 encoding although these errors will not appear, but the saved file name will not be able to read the file, then the parameters can be converted to the operating system to identify the code, encoding conversion can be used mb_convert_encoding (strings, new code, the original code) or Iconv (original code, new Code, string), so that after processing the saved file name will not appear garbled, you can read the file normally, to achieve the Chinese name file upload, download.
In fact, there are better solutions, completely disconnected from the system, you do not have to consider the system is what the code. You can generate a sequence of letters and numbers as a file name, and the original with Chinese name in the database, so call Move_uploaded_file () will not have problems, download the file name only to the original with the Chinese name. The code to implement the download is as follows
Header ("Pragma:public");
Header ("Expires:0″");
Header ("Cache-component:must-revalidate, post-check=0, Pre-check=0″);"
Header ("Content-type: $file _type");
Header ("Content-length: $file _size");
Header ("content-disposition:attachment; Filename=\ "$file _name\");
Header ("Content-transfer-encoding:binary");
ReadFile ($file _path);
$file _type is the type of file, $file _name is the original name, $file _path is the address of the file that is saved on the service.
Four. Let's summarize why it's garbled.
In general, the occurrence of garbled characters have 2 kinds of reasons, the first is because the encoding (charset) set error, causing the browser to parse with the wrong encoding, resulting in a full screen messy "heavenly book", followed by the file is the wrong code to open, and then save, such as a text file was originally GB2312 encoded, It is opened and saved with UTF-8 encoding. To solve the above garbled problem, first need to know which links in the development of the Code:
1, file encoding: Refers to the paging file (. html,.php, etc.) itself is the type of code to save. Notepad and Dreamweaver automatically recognize the file encoding when they open the page, so there is no problem. But Zendstudio will not automatically recognize the code, it will only be fixed according to the preferences of the configuration of a code to open the file, if the work is not noticed, with the wrong code to open the file, made a change after a save, garbled appeared (I have a deep experience).
2, page statement code: In the HTML code head inside, you can use <meta http-equiv= "Content-type" content= "text/html;" charset= "xxx"/> To tell the browser what encoding the Web page, the current Chinese web site in the development of XXX is mainly used in the GB2312 and UTF-8 two kinds of coding.
3, database Connection code: Refers to the database operation when the encoding and database transmission data, here need to pay attention to the database itself is not confused with the coding, such as MySQL internal default is latin1 encoding, that is, MySQL is latin1 encoded to store data, Data transmitted to MySQL in other encodings is converted to latin1 encoding.
Know where the web development involved in coding, also know the reason for the garbled code: the above 3 coding settings are inconsistent, because most of the encoding is compatible with ASCII, so the English symbol will not appear, Chinese on the bad luck.
Five. Decisive battles some common error situations and solutions:
1, the database uses UTF8 code, and the page affirms the code is GB2312, this is the most common generation of garbled reason. At this time in the PHP script directly select data out is garbled, need to use before the query: mysql_query ("SET NAMES GBK"); To set the MySQL connection code to ensure that the page declaration code is consistent with the connection code set here (GBK is an extension of GB2312). If the page is UTF-8 encoded, you can use: mysql_query ("SET NAMES Utf8″);"
Attention is UTF8 rather than general UTF-8. If the code of the page declaration is consistent with the internal encoding of the database, the connection code can be set.
Note: In fact, MySQL's data input and output is more complex than the above, the MySQL configuration file My.ini defines 2 default encodings, respectively, in [client] Default-character-set and [mysqld] Default-character-set to set the default client connection and the code used inside the database. The code we specify above is actually a MySQL Client Connection server command line parameter character_set_client, to tell the MySQL server received the client data is what encoding, rather than the default encoding.
2, page declaration code and the file itself is inconsistent with the code, this situation rarely occurs, because if the code inconsistent with the page when the browser to see is garbled. More often it is after the release to modify some small bugs, to open the page in error encoding and then save the resulting. Or with some FTP software directly online modify files, such as CuteFTP, due to software coding configuration errors caused by the conversion of the wrong encoding.
3, some rented virtual host friends, obviously the above 3 codes are set up correctly or garbled. For example, the Web page is GB2312 code, ie, such as browser Open but always recognized as UTF-8, the page head has been declared GB2312, manually modify the browser code for GB2312 after the page display normal. The reason is that the server Apache set the server global default code, in Httpd.conf added Adddefaultcharset UTF-8. At this time the server will first send HTTP headers to the browser, its priority than the page in the Declaration of High Code, the natural browser to identify the wrong. There are 2 solutions, ask the administrator to add a adddefaultcharset GB2312 to the configuration file's own virtual machine to override the global configuration, or configure it in the. htaccess of your own directory.

Summary: In short, to solve the PHP Chinese garbled the best solution is that the page statement code and the database internal code is consistent, if the page number of pages requested and the database internal coding inconsistent, set the connection code, mysql_query ("Set NAMES XXX"); XXX is a connection code. It will solve the problem of garbled characters.

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.