Chinese file name garbled during php file Upload

Source: Internet
Author: User
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, but I am going to upload an exce

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 the experts will give you advice). If you want to understand this problem, go to the relevant tutorials, simply find the iconv function.

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

Example of use: $ content = iconv ("GBK", "UTF-8", $ content); this example is used to convert $ content from GBK to UTF-8 encoding, garbled key code:

  1. $ Name = iconv ("UTF-8", "gb2312", $ name );
  2. Move_uploaded_file ($ tmpname, $ this-> final_file_path );
  3. $ 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. for example:

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

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.