Home  >  Article  >  Backend Development  >  What should I do if the file name of a Chinese file uploaded by PHP is garbled?

What should I do if the file name of a Chinese file uploaded by PHP is garbled?

coldplay.xixi
coldplay.xixiOriginal
2020-07-18 14:33:573466browse

Solution to the garbled file name of Chinese files uploaded to php: Add relevant statements in the middle of the head tag of the html file to keep the encoding unified. The code is [

Solution to garbled file names of Chinese files uploaded to php:

Method 1: Let me guess, maybe my system will cause garbled characters when creating Chinese files, so I rewrote the code:

move_uploaded_file($_FILES["file"]["tmp_name"], "upload/测试数据.txt");

The result was successfully created without garbled characters. .. In other words, it is not a system problem.

Think about it, my php file itself is utf8 encoded, then

move_uploaded_file($_FILES["file"]["tmp_name"],"upload/测试数据.txt");

this statement must use utf8 encoding, then the previously uploaded file The name is definitely not utf8 encoded, so the following statement is definitely wrong, because the source string itself is not utf8 encoded:

iconv("UTF-8","gbk//TRANSLIT",$_FILES["file"]["name"]);

Use the function to check the encoding of the source string:

$e=mb_detect_encoding($text, array(‘UTF-8’, ‘GBK’,’gb2312’));2 echo $e;

The result is CP936, that is, the source string encoding is GBK.

Try it

move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . iconv("gbk","UTF-8",$_FILES["file"]["name"]));

The problem is solved, no more garbled characters

Method 2: In fact, there is another solution, which is to add

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

in the middle of the head tag of the html file so that the encoding remains unified, and there is no need to transcode anymore

Conclusion

  • Using the iconv function can solve the problem of garbled Chinese file names when uploading. In fact, iconv can solve various garbled problems caused by inconsistent encoding.

  • When using the iconv function, please check the encoding of the source string first, unless you have already determined the encoding of the source string.

  • Try to ensure that the encoding of all codes is consistent , use the iconv function as a last resort.

  • Complain, try not to use Chinese file names as file names saved on the server, please convert the file name into your own file name (even if it is in English Please also convert the file name).

Related learning recommendations: PHP programming from entry to proficiency

The above is the detailed content of What should I do if the file name of a Chinese file uploaded by PHP is garbled?. 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