Php Solution to garbled Chinese file names for uploading files

Source: Internet
Author: User
Tags php file upload recode
Many of my friends may encounter this problem when developing the upload function for php encoded with UTF-8, that is, when uploading a file with a Chinese file name, the file name will become garbled, in fact, we can use the iconv function

Many of my friends may encounter this problem when developing the upload function for php encoded with UTF-8, that is, when uploading a file with a Chinese file name, the file name will become garbled, in fact, we can use the iconv function

Some of my friends may encounter some problems: When uploading files, if the original name is good in English, there will be no problems. If it is Chinese, garbled characters may occur, today, I will summarize the causes and solutions for garbled characters in the Chinese file names of php uploaded files.

In the past few days, XAMPP has been installed in windows, so I am going to learn about php. I have encountered php File Upload problems over the past few days. However, I am going to upload an excel file, but if the file name is a Chinese name, an error will be reported.

One to two go is very depressed, and then think carefully should be a file encoding problem, I write PHP files using UTF-8 encoding, if you haven't guessed it wrong, the APACHE processing should use GBK (of course, I am not sure now. I hope you can give me some advice ). If you want to understand this problem, go to the relevant tutorials and simply find the iconv function.

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

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

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

Key code for Garbled text:

The Code is 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 solving the problem of Chinese garbled characters in the uploaded files, we can rename the uploaded files.

Example

The Code is as follows:


$ SFileName = "sda. php ";
$ SOriginalFileName = $ sFileName;
$ SExtension = s str ($ sFileName, (strrpos ($ sFileName, '.') + 1); // locate the extension
$ SExtension = strtolower ($ sExtension );
$ SFileName = date ("YmdHis "). rand (100,200 ). ". ". $ sExtension; // This is the new file name, and there will be no garbled characters in the full number.



Below are some supplements:

Php Solution for uploading Chinese file names with garbled characters

The Code is as follows:


$ Filepath = "upload /";
$ Name = $ filepath. $ _ FILES ["upfile"] ["name"];
While (file_exists ($ name )){
$ Temp = explode (".", $ name); // split the string
$ Name = $ temp [0]. "0". $ temp [1]; // Add 0 after the main file name
}

The Code is as follows:


// The iconv () function is the key
If (move_uploaded_file ($ _ FILES ["upfile"] ["tmp_name"], iconv ("UTF-8", "gb2312", $ name ))) {// process ...}

My PHP code is UTF-8, probably because the operating system is GBK!

Note: My servers are windows xp and apache. It is estimated that the xp character set is gbk. Because my php code is saved in UTF-8 format, garbled characters may occur when naming the file name, so you can use iconv () the function converts the original UTF-8 file name to gbk format.

Php utf8 encoding solution for garbled characters in Chinese file names uploaded

Many of my friends may encounter this problem when developing the upload function for php encoded with UTF-8, that is, when uploading a file with a Chinese file name, the file name will become garbled, we can use the iconv function to recode the file name to solve the problem.

The Code is 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']); // recode the file name using the Iconv Function
If (move_uploaded_file ($ file ['tmp _ name'], $ name )){
Echo 'file uploaded successfully! ';
Echo 'image information :';
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.