PHP upload file Chinese file name garbled solution

Source: Internet
Author: User

Presumably a lot of friends in the UTF8 encoding php development upload function, will encounter such a problem, that is, uploading Chinese file name files, filenames will become garbled, in fact, we can use the Iconv function to re-encode the file name to solve the problem

may have a lot of friends encountered some problems is to upload the file if the original name is not a problem, if it is Chinese may appear garbled, today I come to you to summarize the cause of garbled php upload file Chinese file name garbled reason and solution it.

XAMPP has been installed under Windows these days and is ready to learn something about PHP. These days to contact the PHP upload file, but there is a depressed problem, I am ready to upload an Excel file, but if the file name is the Chinese name will be error.

Carefree matter is very depressed, and then think carefully think should be the file encoding problem, I write php file using UTF-8 code, if not guess the Apache processing use should be GBK (of course, now I can not be sure, I hope the master to give advice). To understand this problem, to find the relevant tutorial, simply found the iconv this function.

Function Prototypes: String Iconv (String in_charset, String out_charset, String str)

Examples of use: $content = Iconv ("GBK", "UTF-8", $content);

The purpose of this example is to convert $content from GBK to UTF-8 encoding.

Garbled problem key code:

$name=iconv$name); Move_uploaded_file ($tmpname$this,final_file_path); $name=iconv$name);

In addition to this to solve the problem of uploading files in Chinese garbled, we can also rename the upload file.

Cases

$sFileName= "sda.php";$sOriginalFileName=$sFileName;$sExtension= S Str ($sFileName, (Strrpos($sFileName, ‘.‘) + 1));//Find Extension$sExtension=Strtolower($sExtension);$sFileName=Date("Ymdhis").Rand(100, 200). ".".$sExtension;//This is our new file name, all the numbers will not be garbled oh. 

Here are some additions:

PHP upload Chinese file name garbled solution

$filepath= "upload/"$name=$filepath. $_files ["Upfile"] ["Name"while (file_exists($name)) {   $temp=  Explode(".",$name); // Split string    $name=$temp[0]. " 0 ".". ". $temp [1]; // add 0} after the main file name
The Iconv () function is the key
if (Move_uploaded_file ($_files["Upfile" ["Tmp_name"],iconv ("UTF-8", "gb2312", $name))) {//processing ...}

My PHP code is UTF-8, which may be because the operating system is GBK!

Note: My server is Windows XP, Apache, and the estimated XP character set is GBK. Because my PHP code is saved in utf-8 format, in the name of the file name will be garbled, so you can use the Iconv () function to convert the original utf-8 format file name to GBK format.

PHP UTF8 code to upload Chinese file name garbled solution method

Presumably a lot of friends in the UTF8 encoding php development upload function, will encounter such a problem, that is, uploading Chinese file name files, filenames will become garbled, we can use the Iconv function to re-encode the file name to solve the problem.

<?php
Header("Content-type:text/html;charset=utf-8");$submit=$_post[' Submit '];if(isset($submit) &&Trim($submit)! = "){$file=$_files[' File '];if(isset($file[' Tmp_name '])){ $name=Iconv(' Utf-8 ', ' gb2312 ',$file[' name ']);//re-encode file names using the Iconv function if(Move_uploaded_file($file[' Tmp_name '],$name)){ Echo' File Upload succeeded! '; Echo' Picture info: '; Print_r($file); }}}
?

This article turns from http://www.jb51.net/article/42660.htm

PHP upload file Chinese file name garbled solution

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.