Php file upload with Chinese garbled characters, php file name with Chinese garbled characters

Source: Internet
Author: User
Tags php file upload
Php file upload with Chinese garbled characters, php file name with Chinese garbled characters

Php Upload code:

  1. If ($ _ FILES ["file"] ["error"]> 0)
  2. {
  3. Echo "Return Code:". $ _ FILES ["file"] ["error"]."
    ";
  4. } Else
  5. {
  6. Echo "Upload:". $ _ FILES ["file"] ["name"]."
    ";
  7. Echo "Type:". $ _ FILES ["file"] ["type"]."
    ";
  8. Echo "Size:". ($ _ FILES ["file"] ["size"]/1024). "Kb
    ";
  9. Echo "Temp file:". $ _ FILES ["file"] ["tmp_name"]."
    ";
  10. If (file_exists ("upload/". $ _ FILES ["file"] ["name"])
  11. {
  12. Echo $ _ FILES ["file"] ["name"]. "already exists .";
  13. }
  14. Else
  15. {
  16. Move_uploaded_file ($ _ FILES ["file"] ["tmp_name"],
  17. "Upload/". $ _ FILES ["file"] ["name"]);
  18. }
  19. }

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

The following is an analysis of the causes of garbled php file uploads and provides some reliable solutions.

Set:

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

To:

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.

For example:

Test:

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" means that the Chinese text cannot be converted, or even the nearly connected characters do not exist. it seems that the methods introduced on the Internet are not omnipotent.

3. failed to introduce the method on the internet. try again to guess that the system may garbled during the creation of a Chinese file and modify the code:

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

The result is successfully created with no garbled characters. Not a system problem.

Php files are UTF-8 encoded.

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:

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

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

$ E = mb_detect_encoding ($ text, array ('utf-8', 'gbk', 'gb2312 '); echo $ e;

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

Test:

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

Solve the problem and there will be no Chinese garbled characters.

4. for other solutions, add the following content to the head tag of an html file:

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

5. The iconv function can solve the problem of garbled characters in uploaded Chinese file names. In fact, iconv can solve various garbled characters caused by inconsistent codes. 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 to your own file name (even the 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.