Home  >  Article  >  Backend Development  >  Solutions to PHP encoding and Chinese garbled problems

Solutions to PHP encoding and Chinese garbled problems

WBOY
WBOYOriginal
2016-07-25 08:53:54717browse
  1. [mysql]
  2. default-character-set=utf8
  3. [mysqld]
  4. default-character-set=utf8
  5. default-storage-engine=myisam
  6. Add under [mysqld]:
  7. default-collation=utf8_bin
  8. init_connect='set names utf8'
Copy code

2. Add mysql_query("set names 'encoding'"); before the PHP program that needs to do database operations. The encoding is consistent with the PHP encoding. If the PHP encoding is gb2312, then the mysql encoding is gb2312. If it is utf-8, then the mysql encoding is utf8, so that there will be no garbled characters when inserting or retrieving data

Three, php is related to the operating system

The encoding of Windows and Linux is different. In the Windows environment, if the parameters are UTF-8 encoded when calling PHP functions, errors will occur, such as move_uploaded_file(), filesize(), readfile(), etc. These functions are processing It is often used when uploading and downloading, and errors may occur when calling: 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 these errors will not occur when using gb2312 encoding in the Linux operating system, the saved file name will be garbled and the file cannot be read. In this case, you can first convert the parameters into the encoding recognized by the operating system. For encoding conversion, you can use mb_convert_encoding (character String, new encoding, original encoding) or iconv (original encoding, new encoding, string), so that the file name saved after processing will not be garbled, and the file can be read normally to achieve uploading and downloading of Chinese name files.

The better solution is to completely disconnect from the system, without considering the encoding of the system. You can generate a sequence of only letters and numbers as the file name, and save the original name with Chinese characters in the database. In this way, there will be no problem when calling move_uploaded_file(). When downloading, you only need to change the file name to the original name with Chinese characters. name.

Example, code to implement downloading:

  1. header("pragma: public");
  2. header("expires: 0");
  3. header("cache-component: must-revalidate, post-check=0, pre- check=0");
  4. header("content-type: $file_type");
  5. header("content-length: $file_size");
  6. header("content-disposition: attachment; filename="$file_name"") ;
  7. header("content-transfer-encoding: binary");
  8. readfile($file_path);
Copy code

$file_type is the type of file, $file_name is the original name, $file_path is the file saved in The address of the file on the service.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn