PHP upload Chinese file name garbled problem

Source: Internet
Author: User
Tags translit
PHP upload file is the most basic of a technical point, but in-depth there are many problems to be solved, this does not, after uploading the Chinese file, the file name becomes garbled.

Here is the problem code, very simple:

1. Problem code

HTML section:

1  2  3  

PHP section:

1 
 
   0) 3 {4     echo "Return Code:". $_files["File" ["Error"]. "
"; 5}else 7 {8 echo "Upload:". $_files["File" ["Name"]. "
"; 9 echo "Type:". $_files["File" ["type"]. "
"; echo" Size: ". ($_files["File" ["Size"]/1024). The Kb
"; echo" Temp file: ". $_files["File" ["Tmp_name"]. "
";" if (file_exists ("upload/"). $_files["File" ["Name"])) ([]) ("File"] ["name"] in the $_files[. "already exists."; }17 else18 { move_uploaded_file ($_files["file"] ["Tmp_name"],20 "upload/" . $_files ["File"] ["name"]); } }

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

2. Preliminary examination

Search the solution online to

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

Change into

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.



Try it:

1 Var_dump (Iconv ("UTF-8", "Gbk//translit", $_files["file" ["Name"])), 2 Var_dump (Iconv ("UTF-8", "Gbk//ignore", $_ files["File" ["Name"]));

Results:

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 online introduction is not omnipotent.

3. Online Introduction method failed, try again

Guess, perhaps my system in the creation of Chinese files will be garbled, so I rewrite the code:

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

Results created successfully, no garbled ... This is not a system problem.

Imagine that my 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:

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

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

Try it.

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

Problem solving, no longer garbled

4. Another solution

There is actually a workaround, which is to add the HTML file to the head tag

 
  

So that the code remains unified, there is no need to transcode

5. The following is the conclusion

    1. Using the Iconv function can solve the problem of uploading Chinese file names garbled, in fact, iconv can solve a variety of codes due to the lack of uniformity caused by garbled problems.
    2. Use the Iconv function to check the encoding of the source string first, unless you have determined the encoding of the source string.
    3. Try to ensure that all code is coded consistently, using the ICONV function only as a last resort.
    4. Spit it out, try not to use the Chinese file name as the file name that is saved on the server, please convert the file name to your own filename (even if the English file name is translated).
  • 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.