Php solution to garbled Chinese file names for uploading files _ PHP Tutorial

Source: Internet
Author: User
Tags php file upload recode
Php solves the problem of garbled Chinese file names when uploading files. 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 give you a summary. some of my friends may encounter some problems, that is, when uploading files, if the original name is better 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.