PHP File Upload Chinese garbled what to do, PHP file name Chinese garbled problem

Source: Internet
Author: User
Tags php file upload translit
Copy Code

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). The 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. }
Copy Code

uploaded a file named "Test data. txt" file, oh ho, the file is passed up, but the file name is garbled.

The following is the php file upload garbled reason analysis, and give some reliable solutions.

Will:

Move_uploaded_file ($_files["file"] ["Tmp_name"], "upload/". $_files["File" ["name"]);

Modified to:

Move_uploaded_file ($_files["file"] ["Tmp_name"], "upload/". Iconv ("UTF-8", "GBK", $_files["file" ["Name"]));

The result is that the return value of the Iconv function is false. Check the function manual, found that the second parameter has a special use, simple translation is I can add//translit or//ignore after the code, the former will be unable to translate the characters to the nearest character, the latter is directly ignore the characters can not be converted.

Such as:

Test:

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, the Chinese can not be converted, and even close to the characters are not, it seems that the method of the online introduction is not omnipotent.

3, Online Introduction method failure, and then try to guess, perhaps the system in the creation of Chinese files will be garbled, modify the code:

Move_uploaded_file ($_files["file"] ["Tmp_name"], "upload/test data. txt");

The result was successfully created without garbled characters. Not a system problem.

The php file itself is UTF8 encoded, then

Move_uploaded_file ($_files["file"] ["Tmp_name"], "upload/test data. txt");

This statement is sure to use UTF8 encoding, then the file name that was uploaded is definitely not UTF8 encoded, then 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 ')); Echo $e;

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

Test:

Move_uploaded_file ($_files["file"] ["Tmp_name"], "upload/". Iconv ("GBK", "UTF-8", $_files["file" ["Name"]));

Problem solved, no longer have the Chinese garbled problem.

4, other solutions, in the HTML file in the middle of the head tag to add:

So that the code remains uniform, there is no need to re-transcode.

5, Summary use ICONV function can solve the problem of uploading Chinese file name garbled, in fact, iconv can solve a variety of codes due to the lack of uniformity caused by garbled problems. Use the Iconv function to check the encoding of the source string first, unless you have determined the encoding of the source string. Try to ensure that all code is coded consistently, using the ICONV function only as a last resort.

Try not to use the Chinese filename as the file name saved on the server, please convert the file name to your own filename (even if the English file name is translated).

  • Related Article

    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.