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 "", 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 "", all files are encoded in the format of 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
Copy code code as follows:
[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
Copy code code 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.
Turn from: http://www.jb51.net/article/21118.htm