PHP Chinese file name output garbled solution

Source: Internet
Author: User
Chinese file name garbled characters in php are generally for beginners, because we generally do not use Chinese names, because we all know that php is unfriendly to Chinese support, so how can we solve the php Chinese file name garbled problem? The following small series will help you sort out some methods... chinese file name garbled characters in php are generally for beginners, because we generally do not use Chinese names, because we all know that php is unfriendly to Chinese support, so how can we solve the php Chinese file name garbled problem? The following small series will help you sort out some methods. let's take a look at them.

The reason is the encoding problem, so transcoding is required. The iconv function in PHP can solve the problem:

Iconv ("the encoding used currently, such as UTF-8", "the encoding to be converted, such as GB2312", "file name ");

The php instance code is as follows:

$ File_name = "my file .jpg"; $ file_name = iconv ("UTF-8", "gb2312", $ file_name); // solve Chinese garbled echo '$ file_name ';

Example:

 

Another problem to solve Chinese garbled characters is that the code is as follows:

$ SFileName = "sda. php "; $ sOriginalFileName = $ sFileName; $ sExtension = s str ($ sFileName, (strrpos ($ sFileName ,'. ') + 1); // find 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.

We can also use urlencode for compilation, such as urlencode ('China'). The example code is as follows:

  

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, therefore, you can use the iconv () function to convert the original UTF-8 file name to gbk format.

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.