The Chinese file name of the php uploaded file is garbled. 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
$ 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.
...