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.