Solution to php upload file Chinese file name confusion code

  • 2020-11-03 22:04:15
  • OfStack

There may be some problems encountered by many friends. If the file is uploaded in English, the original name will not be a problem; if it is uploaded in Chinese, the original name may be confused. Today, I would like to summarize the reasons for the confusion and the solutions for php's uploading files.

These days, XAMPP has been installed under windows, and I am ready to learn the contents related to php 1 preliminarily. I contacted php to upload a file these days, but there was a frustrating problem. I plan to upload an excel file, but if the file name is Chinese, an error will be reported.

Later, I thought carefully that it should be the file code. The php file I wrote used the code of UTF-8. If I guess correctly, it should be GBK (of course, I'm not sure now, I hope you can give me your advice). To figure this out, I went to the relevant tutorial and found the iconv function.

Function prototype: string iconv (string in_charset, string out_charset, string str)

Example: $content = iconv("GBK", "ES33en-8 ", $content);

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

Key code:


$name=iconv("UTF-8","gb2312", $name);
move_uploaded_file($tmpname, $this->final_file_path); 
$name=iconv("gb2312","UTF-8", $name);

In addition to this solution to upload the file Chinese disorderly code problem, we can also upload the file renamed.

case


$sFileName = "sda.php";
$sOriginalFileName = $sFileName;
$sExtension = s str($sFileName, (strrpos($sFileName, '.') + 1));// Find the extension 
$sExtension = strtolower($sExtension);
$sFileName = date("YmdHis").rand(100, 200).".".$sExtension; // This is our new file name, the full number of no messy oh. 


Here are some additions:

php upload Chinese file name scrambling solution


 $filepath="upload/";
 $name=$filepath.$_FILES["upfile"]["name"];
 while(file_exists($name)){
   $temp=explode(".",$name);// Split string 
    $name=$temp[0]."0".".".$temp[1];// Add after the main file name 0
 }


//iconv() The function is the key 
  if(move_uploaded_file($_FILES["upfile"]["tmp_name"],iconv("UTF-8","gb2312",$name))){// To deal with ...}

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

Note: My server is windows xp and apache, so the xp character set is estimated to be gbk. Since my php code is saved in ES76en-8 format, there will be some confusion when naming the file name, so I can use the iconv() function to convert the original utf-8 file name to gbk format.

php utf8 encoding upload Chinese file name appears the solution of garbled code

Presumably, many friends will encounter such a problem when developing and uploading php with utf8 encoding. When uploading the Chinese file name, the file name will become a mess. We can use iconv function to recode 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']); // using Iconv Function to recode file names 
     if(move_uploaded_file($file['tmp_name'],$name)){
    echo ' File upload successful !';
    echo ' Picture information: ';
    print_r($file);
   }
}
}
?>


Related articles: