PHP Upload files Chinese filename garbled solution _php Tips

Source: Internet
Author: User

There may be a lot of friends have encountered some problems is the upload file if the original name will not have a problem, if it is Chinese may appear garbled, today I would like to give you a summary of the cause of garbled php file files in Chinese filename garbled reasons and solutions.

These days in Windows installed XAMPP, ready to preliminary learning about PHP content. 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 filename is the Chinese name will be an error.

Carefree matter is very depressed, and then carefully think about it should be file encoding problem, I write PHP file using is UTF-8 code, if not guessed wrong Apache processing 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 tutorials, simply found the Iconv function.

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

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

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

Garbled problem key code:

Copy Code code as follows:

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

In addition to this to solve the upload file Chinese garbled problem, we can also upload file renamed can be.

Cases

Copy 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 filename, the full number of no garbled oh.


Here are some additions:

PHP upload Chinese file name garbled solution

Copy 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 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 encoding 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 will appear garbled, so you can use the Iconv () function of the original utf-8 format of the file name into the GBK format.

PHP UTF8 code upload Chinese file name appears garbled solution

Presumably a lot of friends in the UTF8 Code PHP development upload function, will encounter such a problem, that is, upload Chinese file name files, file name will become garbled, we can use the Iconv function to the file name to be encoded to solve the problem.

Copy Code code as follows:

<?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 with the Iconv function
if (Move_uploaded_file ($file [' Tmp_name '], $name)) {
echo ' File upload successful! '
echo ' picture info: ';
Print_r ($file);
}
}
}
?>

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.