Home > Backend Development > PHP Tutorial > Solution to the problem of garbled Chinese file names uploaded by PHP, _PHP tutorial

Solution to the problem of garbled Chinese file names uploaded by PHP, _PHP tutorial

WBOY
Release: 2016-07-13 10:07:57
Original
1098 people have browsed it

Solution to solve the problem of garbled Chinese file names uploaded by PHP,

Uploading files in PHP is the most basic technical point, but there are many problems that need to be solved when going deeper. No, after uploading a Chinese file, the file name becomes garbled.

The following is the problem code, it is very simple:

1. Problem code

html part:

Copy code The code is as follows:



enctype="multipart/form-data">








php part:

Copy code The code is as follows:

if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "
";
}else
{
echo "Upload: " . $_FILES["file"]["name"] . "
";
echo "Type: " . $_FILES["file"]["type"] . "
";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb
";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "
";
If (file_exists("upload/" . $_FILES["file"]["name"]))
{
              echo $_FILES["file"]["name"] . " already exists. ";
}
      else
{
       move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
}
}

Uploaded a file named "test data.txt", oh ho, the file was uploaded, but the file name was garbled.

2. First test

Search for solutions online and find

Copy code The code is as follows:

move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);

changed to

Copy code The code is as follows:

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

It turns out that the return value of iconv function is false.

Check the function manual and find that the second parameter has special usage. A simple translation is that I can append //TRANSLIT or //IGNORE after the encoding. The former will convert the untranslatable characters into the closest characters. , the latter simply ignores characters that cannot be converted.

Try it:

Copy code The code is as follows:

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

Result:

bool(false) string(4) ".txt"

That is to say, Chinese characters cannot be converted, and there are not even close characters. It seems that the methods introduced online are not omnipotent.

3. The online introduction method fails, try again

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

Copy code The code is as follows:

move_uploaded_file($_FILES["file"]["tmp_name"], "upload/test data.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

Copy code The code is as follows:

move_uploaded_file($_FILES["file"]["tmp_name"],"upload/test data.txt");

This statement must use utf8 encoding, so the file name uploaded before must not be utf8 encoded, then the following statement must be wrong, because the source string itself is not utf8 encoded:

Copy code The code is as follows:

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

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

Copy code The code is as follows:

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

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

Try it

Copy code The code is as follows:

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

Problem solved, no more garbled characters

4. Another solution

In fact, there is another solution, which is to add

in the middle of the head tag of the html file.

Copy code The code is as follows:


This keeps the encoding consistent and eliminates the need to transcode

5. The following is the 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.
Please check the encoding of the source string first when using the iconv function, unless you have already determined the encoding of the source string.
Try to ensure that all codes are encoded consistently, and only use the iconv function as a last resort.
To make a complaint, try not to use Chinese file names as file names saved on the server. Please convert the file names into your own file names (please convert even English file names).

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/953152.htmlTechArticleSolution to solve the problem of garbled Chinese file names uploaded by php. Uploading files in php is the most basic technical point, but in-depth There are a lot of problems that need to be solved after entering. No, after uploading the Chinese file...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template