Perfect solution to PHP Chinese garbled problem _php tutorial

Source: Internet
Author: User
Tags mysql code readfile
First, the code of the PHP Web page

The encoding of the 1.php file itself should match the encoding of the Web page

A. If you want to use gb2312 encoding, then PHP will output headers: header ("content-type:text/html; charset=gb2312 "), static page add, all files encoded in the format ANSI, can be opened with Notepad, save as select encoding for ANSI, overwrite source file.

B. If you want to use UTF-8 encoding, then PHP will output headers: header ("content-type:text/html; Charset=utf-8 "), static pages are added, all files are encoded in UTF-8 format. Save As Utf-8 may be a bit troublesome, general Utf-8 file will have a BOM at the beginning, if the use of the session will be a problem, can be saved EditPlus, in the EditPlus, tools---parameter selection, file->utf-8 signature, Select Always Delete, and then save the BOM information can be removed.

The 2.php itself is not Unicode, and all functions such as substr are changed to MB_SUBSTR (mbstring extensions are required) or transcoded with Iconv.

Two. PHP's data interaction with MySQL

PHP and database encoding should be consistent

1. Modify the MySQL configuration file My.ini or My.cnf,mysql best with UTF8 encoding

[MySQL]

Default-character-set=utf8

[Mysqld]

Default-character-set=utf8

Default-storage-engine=myisam

Under [Mysqld], add:

Default-collation=utf8_bin

Init_connect=set NAMES UTF8

2. Add mysql_query ("Set names Code") before the PHP program that needs to do the database operation, and the code is consistent with PHP code, if PHP code is gb2312 that MySQL code is gb2312, if it is utf-8 then the MySQL code is UTF8 So that you don't get garbled when inserting or retrieving data.

Three. PHP is related to the operating system

The encoding of Windows and Linux is not the same, in the Windows environment, the function of invoking PHP when the parameter is UTF-8 encoding will be error, such as Move_uploaded_file (), FileSize (), ReadFile (), etc. These functions are often used when handling uploads and downloads, and the following 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 ...

Warning:filesize () [Function.filesize]: Stat failed for ...

Warning:readfile () [Function.readfile]: Failed to open stream:invalid argument in.

In the Linux environment with GB2312 encoding although these errors will not occur, but the file name after the save is garbled resulting in unreadable files, you can first convert the parameters to the operating system identification code, encoding conversion can be used mb_convert_encoding (string, new code, the original code) or Iconv (original code, new Code, string), so that the file name stored after processing will not appear garbled, can also read the file, to achieve the Chinese name file upload, download.

In fact, there is a better solution, complete with the system out, also do not have to consider what the system is coding. Can generate a sequence of only letters and numbers as the file name, and the original with the Chinese name in the database, so call Move_uploaded_file () There is no problem, the download only need to change the file name to the original Chinese name. 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 file type, $file _name is the original name, $file _path is the address of the file that is saved on the service.

http://www.bkjia.com/PHPjc/486438.html www.bkjia.com true http://www.bkjia.com/PHPjc/486438.html techarticle First, the encoding of the PHP Web page 1.php file itself and the encoding of the page should match a. If you want to use gb2312 encoding, then PHP will output headers: header (content-type:text/html; Charse ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.