How to solve the garbled file name in PHP files

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

PHP files use UTF-8 encoding, if there is no wrong guess Apache processing should be GBK. 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:
Copy the code code as follows:

$name=iconv("UTF-8","gb2312"$name);

In addition to this to solve the problem of uploading files in Chinese garbled, we can also rename the upload file.
Cases
Copy the code code as follows:

$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
Copy the code code as follows:

$filepath = "upload/";
$name = $filepath. $_files["Upfile" ["Name"];
while (File_exists ($name)) {
$temp =explode (".", $name);//Split string
$name = $temp [0]. " 0″. ".". $temp [1];//main file name plus 0
}
Copy the code code as follows:

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.
Copy the code code as follows:

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‘]); //利用Iconv函数对文件名进行重新编码
     if(move_uploaded_file($file[‘tmp_name‘],$name)){
    echo‘文件上传成功!‘;
    echo‘图片信息:‘;
    print_r($file);
   }
}
}

How to solve the garbled file name in PHP files

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.