Php solution for garbled characters in uploaded Chinese file names

Source: Internet
Author: User
This article mainly introduces how php uploads garbled characters in Chinese file names. For more information about php file uploading, however, there are also many problems to solve. this is not the case. after a Chinese file is uploaded, the file name becomes garbled.

The following is the problem code, which is simple:

1. problem Code

Html section:

The code is as follows:







Php section:

The code is as follows:


<? Php
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"]);
}
}

Upload a file named "testing data .txt", oh ho, the file is uploaded, but the file name is garbled.

2. initial test

Search for the solution on the Internet

The code is as follows:


Move_uploaded_file ($ _ FILES ["file"] ["tmp_name"], "upload/". $ _ FILES ["file"] ["name"]);

Change

The code is as follows:


Move_uploaded_file ($ _ FILES ["file"] ["tmp_name"], "upload /". iconv ("UTF-8", "gbk", $ _ FILES ["file"] ["name"]);

The returned value of the iconv function is false.

Check the function manual and find that the second parameter has a special usage. a simple translation is that I can append // Transcoder or // IGNORE after the encoding, the former converts untranslated characters into the nearest character, while the latter directly ignores unconvertible characters.

Try:

The code is as follows:


Var_dump (iconv ("UTF-8", "gbk // transcoder", $ _ FILES ["file"] ["name"]);
Var_dump (iconv ("UTF-8", "gbk // IGNORE", $ _ FILES ["file"] ["name"]);

Result:

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

That is to say, the Chinese text cannot be converted, or even the characters that are near to each other are not. it seems that the methods introduced on the Internet are not omnipotent.

3. failed to introduce the method online. try again

I guess my system may be garbled when creating a Chinese file, so I changed the code:

The code is as follows:


Move_uploaded_file ($ _ FILES ["file"] ["tmp_name"], "upload/test data .txt ");

The result is successfully created with no garbled characters... That is to say, it is not a system problem.

Think about it, my php file is UTF-8 encoded, so

The code is as follows:


Move_uploaded_file ($ _ FILES ["file"] ["tmp_name"], "upload/test data .txt ");

This statement must be UTF-8 encoded, so the uploaded file name must not be UTF-8 encoded. the following statement must be incorrect because the source string itself is not UTF-8 encoded:

The code is as follows:


Iconv ("UTF-8", "gbk // transcoder", $ _ FILES ["file"] ["name"]);

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

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

The code is as follows:


Move_uploaded_file ($ _ FILES ["file"] ["tmp_name"], "upload /". iconv ("gbk", "UTF-8", $ _ FILES ["file"] ["name"]);

Solve the problem without garbled characters

4. another solution

In fact, there is another solution, that is, adding the header tag in the html file

The code is as follows:



In this way, the encoding remains uniform and no transcoding is required.

5. The following is the conclusion:

The iconv function can solve the problem of garbled characters in the uploaded Chinese file names. In fact, iconv can solve various garbled characters caused by inconsistent encoding.
To use the iconv function, check the encoding of the source string unless you have determined the encoding of the source string.
Make sure that all codes are encoded in the same way, and use the iconv function as a last resort.
Try not to use the Chinese file name as the file name saved on the server. convert the file name into your own file name (even if it is an English file name ).

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.