I. First, PHP webpage code
1. The php file encoding should match the webpage encoding.
A. if you want to use gb2312 encoding, php needs to output the header ("Content-Type: text/html; charset = gb2312") and add it to the static page, the encoding format of all files is ANSI, which can be opened in Notepad. Save it as ANSI and overwrite the source file.
B. if you want to use UTF-8 encoding, php needs to output headers: header ("Content-Type: text/html; charset = UTF-8") and add static pages, the encoding format of all files is UTF-8. It may be a little troublesome to save it as UTF-8. Generally, BOM is generated at the beginning of the UTF-8 file. If session is used, problems may occur. You can use editplus to save it in editplus, tool-> Parameter Selection-> file-> UTF-8 signature, select always delete, and then save to remove the BOM information.
2. php itself is not Unicode. All functions such as substr must be changed to mb_substr (mbstring extension is required), or iconv transcoding is used.
Ii. Data Interaction between PHP and Mysql
PHP and database encoding should be consistent
1. Modify the mysql configuration file my. ini or my. cnf. It is best to use utf8 encoding for mysql.
[Mysql]
Default-character-set = utf8
[Mysqld]
Default-character-set = utf8
Default-storage-engine = MyISAM
Add the following under [mysqld:
Default-collation = utf8_bin
Init_connect = set names utf8
2. add mysql_query ("set names encoding") before the php program that requires database operations. The encoding is consistent with the php code. If the php code is gb2312, the mysql code is gb2312, if it is UTF-8, mysql encoding is utf8, so no garbled characters will appear during data insertion or retrieval.
Iii. PHP related to the Operating System
The encoding for Windows and Linux is different. In Windows, if the parameter is UTF-8 encoded when a function of PHP is called, an error occurs, such as move_uploaded_file (), filesize (), and readfile () these functions are often used for processing uploads and downloads. The following errors may occur during calls:
Warning: move_uploaded_file () [function. move-uploaded-file]: failed to open stream: Invalid argument in...
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 ..
Although gb2312 encoding in Linux does not produce these errors, the stored file name becomes unreadable due to garbled characters. In this case, you can first convert the parameter to the encoding recognized by the operating system, encoding conversion can be performed using mb_convert_encoding (string, new encoding, original encoding) or iconv (original encoding, new encoding, string). In this way, the stored file name will not contain garbled characters, you can also normally read files to upload and download files with Chinese names.
In fact, there are still better solutions to completely break away from the system, so you don't have to consider the encoding of the system. You can generate a sequence with only letters and numbers as the file name, and store the original Chinese name in the database. In this way, calling move_uploaded_file () will not cause problems, during the download, you only need to change the file name to the original name with Chinese characters. The download code 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 file type, $ file_name is the original name, and $ file_path is the address of the file stored on the service.