Home  >  Article  >  Backend Development  >  What to do if php Chinese is sent to the background with garbled characters

What to do if php Chinese is sent to the background with garbled characters

藏色散人
藏色散人Original
2021-12-21 09:59:271561browse

Solution to the garbled Chinese characters transmitted to the background in php: 1. Use "iconv('UTF-8', 'GB2312//IGNORE', $targetFile);"; 2. Use "move_uploaded_file($tempFile, $targetFile);".

What to do if php Chinese is sent to the background with garbled characters

The operating environment of this article: Windows 7 system, PHP version 7.1, Dell G3 computer.

What should I do if Chinese php is sent to the background with garbled characters?

Solution to the Chinese garbled problem of php file upload background storage:

php file upload background processing explanation:

After the user uploads the file ( I use uploadify2.3 for the front end. I hope to save it according to its original file name (sometimes in Chinese). Only using PHP's move_uploaded_file command will cause garbled characters (I am a Ubuntu server). You need to use icotargetFile=iconv(′UTF−8′,′GB2312//IGNORE′,targetFile) first; without IGNORE, iconv will fail to convert the character - (horizontal bar) and automatically truncate it afterwards. For example:

iconv('UTF-8', 'GB2312//IGNORE', "博客—yet"); // 输出"博客yet"
iconv('UTF-8', 'GB2312//IGNORE', "博客—yet"); // 输出"博客"

So the correct storage method is:

$targetFile = iconv('UTF-8', 'GB2312//IGNORE', $targetFile); // UTF8转GB2312
move_uploaded_file($tempFile, $targetFile);  // 服务器把文件暂时放在$tempFile

Because transcoding will lose characters, we need to save the new file name. In order to determine which characters are lost, I used this back and forth Conversion trick:

$targetFile = iconv('GB2312', 'UTF-8//IGNORE', $targetFile );

php file download background processing explanation:

When to use GB2312 and when to use UTF8 is important.

In addition, ob_clean() and flush() are the key to whether the downloaded file is garbled

The following is the file upload backend file I use with uploadify 2.3, for reference only:

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What to do if php Chinese is sent to the background with garbled characters. For more information, please follow other related articles on the PHP Chinese website!

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