Perfect solution to PHP Chinese garbled code

  • 2020-03-31 17:09:58
  • OfStack

one The first is the coding of PHP web pages
1. The encoding of the PHP file itself should match that of the web page
A. If you want to use gb2312 code, then PHP output header: header(" content-type: text/ HTML; Charset =gb2312"), static page add < Meta HTTP - equiv = "content-type" Content = "text/HTML. Charset = gb2312 "> , the encoding format of all files is ANSI, can be opened by notepad, save as selected encoding as ANSI, overwrite the source file.
B. If you want to use utf-8 encoding, PHP will output a header: 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 utf-8. Saving as utf-8 can be a bit of a hassle. Generally, utf-8 files start with a BOM. If you use session, you will have problems. Parameter selection -> File - > Utf-8 signature, select always delete, save to remove BOM information.
2. PHP itself is not Unicode, all substr functions have to be changed to mb_substr (need to install mbstring extension); Or iconv transcoding.
two PHP interacts with Mysql's data
PHP should be coded the same as the database
1. Modify the mysql configuration file my.ini or my.cnf. Mysql is best encoded in utf8
 
[mysql] 
default-character-set=utf8 
[mysqld] 
default-character-set=utf8 
default-storage-engine=MyISAM 
 in [mysqld] The join : 
default-collation=utf8_bin 
init_connect='SET NAMES utf8' 

2. Add mysql_query("set names' code '") to PHP programs that need to do database operations; If the PHP code is gb2312, then the mysql code is gb2312, and if it is utf-8, then the mysql code is utf8. In this way, when inserting or retrieving data, there will be no messy code
3. PHP is OS specific
Windows and Linux are not the same encoding, in the Windows environment, when calling PHP function if the parameter is utf-8 encoding will be wrong, such as move_uploaded_file(), filesize(), readfile(), etc., these functions are often used to handle upload, download, call may appear the following error:
Warning: the move_uploaded_file () function. The move uploaded - file] : failed to open stream: Invalid argument in...
Warning: the move_uploaded_file () function. The move uploaded - file] : Unable to move ' 'to' in...
Warning: filesize() [function.filesize]: stat failed for... The in...
Warning: readfile() [function.readfile]: failed to open stream: Invalid argument in..
In the Linux environment in gb2312 encoding won't appear these mistakes, but save the file name after garbled words can't read the file, then can transform parameters into the operating system first recognition of coding, coding conversion available mb_convert_encoding (string, new coding, the original code) or iconv (the original code, new coding, string), so the file name will not save the garbled words, can also read the file normally, realizes the Chinese name of the file upload, download.
There's a better solution, which is to completely disconnect from the system, so you don't have to worry about what the system is coded. You can generate a sequence of letters and Numbers as the file name, save the original Chinese name in the database, and call move_uploaded_file() without problems, just change the file name to the original Chinese name when downloading. 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 the file, $file_name is the original name, and $file_path is the address of the file saved on the service.

Related articles: