I. first, PHP webpage code 1. the PHP file encoding should match the webpage encoding. if you want to use gb2312 encoding, php needs to output the header: header (Content-Type: text/html; charset = gb2312) and add static pages. all files are encoded in ANSI format, open it in Notepad and save it as an ANSI code to overwrite the source file. ">
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 'code'") 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.