PHP upload Chinese file name garbled problem, _php tutorial

Source: Internet
Author: User
Tags translit

PHP upload Chinese file name garbled problem,


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 <HTML>2 <Body>3 4 <formAction= "upload_file.php"Method= "POST"5 enctype= "Multipart/form-data">6 <label for= "File">Filename: 
    label>  7 <input  type= "File" name = "File" ID = "File" /> 8 <br  />  9 <input  type  = "Submit  " name= " submit" value= "Submit" />  Ten  
     form>   
      Body  >  
       html> 
     
    
   
 

PHP section:

1 
 Php2 if($_files["File"] ["error"] > 0)3 {4     Echo"Return Code:".$_files["File"] ["Error"]. "
";5 }Else7 {8 Echo"Upload:".$_files["File"] ["Name"]. "
";9 Echo"Type:".$_files["File"] ["Type"]. "
";Ten Echo"Size:". ($_files["File"] ["Size"]/1024). The Kb
"; One Echo"Temp file:".$_files["File"] ["Tmp_name"]. "
"; A - if(file_exists("upload/".$_files["File"] ["Name"])) - { the 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" 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$_files["File"] ["name"]);

Change into

Move_uploaded_file ($_filesiconv("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 ($textarray(' UTF-8 ', ' GBK ',' gb2312 ')); 2 Echo $e;

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

Try it.

Move_uploaded_file ($_filesiconv("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

<http-equiv= "Content-type"  Content= "text/html; charset= Utf-8 "/>

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

5. The following is the conclusion

http://www.bkjia.com/PHPjc/949209.html www.bkjia.com true http://www.bkjia.com/PHPjc/949209.html techarticle PHP upload Chinese file name garbled problem, PHP upload file is the most basic of a technical point, but in-depth there are many problems need to be solved, this does not, after uploading the Chinese file, the text ...

  • 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.