没有不值得去解决的问题,也没有不值得去学习的技术!

Fix File Rename Errors for Chinese Filenames on Windows 10 64-bit and PHP 5.6

Error: rename(E:/wwwroot/avatar/BEIJI/侯森.jpg,E:/wwwroot/avatar/BEIJI/378477.jpg): ϵͳ�Ҳ���ָ�����ļ��� (code: 2)

1. Rename a file in a directory. The code is shown in Figure 1.

Rename a file in a directory
Figure 1

2. Error: rename(E:/wwwroot/avatar/BEIJI/侯森.jpg,E:/wwwroot/avatar/BEIJI/378477.jpg): ϵͳ�Ҳ���ָ�����ļ��� (code: 2), as shown in Figure 2.

Error: rename(E:/wwwroot/avatar/BEIJI/侯森.jpg,E:/wwwroot/avatar/BEIJI/378477.jpg): ϵͳ�Ҳ���ָ�����ļ��� (code: 2)
Figure 2

3. Use the file_exists function to check if the file exists. If it exists, print 1; if not, print 0, as shown in Figure 3.

Use the file_exists function to check if the file exists. If it exists, print 1; if not, print 0
Figure 3

4. The printed result is 0, indicating that the file check result is non-existent, as shown in Figure 4.

The printed result is 0, indicating that the file check result is non-existent
Figure 4

5. Open the URL: http://php.net/manual/zh/function.rename.php , and find a tip: , to solve this kind of problem, as shown in Figure 5
Translation: On windows (XP, vista, 7 …) http://fr.wikipedia.org/wiki/Windows-1252″, if your filename contains accents, it basically doesn’t work, so use the iconv function from utf-8 to cp1252 as follows:

On windows (XP, vista, 7 ...) http://fr.wikipedia.org/wiki/Windows-1252", if your filename contains accents, it basically doesn't work, so use the iconv function from utf-8 to cp1252 as follows:
Figure 5

6. iconv — Convert string to requested character encoding. Convert the string E:/wwwroot/avatar/BEIJI/侯森.jpg from UTF-8 encoding to GB2312. The code is shown in Figure 6.

iconv — Convert string to requested character encoding. Convert the string E:/wwwroot/avatar/BEIJI/侯森.jpg from UTF-8 encoding to GB2312
Figure 6

7. The printed result is 1, indicating that the file check result is existent, as shown in Figure 7.

The printed result is 1, indicating that the file check result is existent
Figure 7

8. The final implementation code is shown in Figure 8.

The final implementation code
Figure 8

9. The files in the directory E:/wwwroot/avatar/BEIJI are shown in Figure 9.

The files in the directory E:/wwwroot/avatar/BEIJI
Figure 9

10. Run the script. The file in the directory E:/wwwroot/avatar/BEIJI has been successfully renamed, as shown in Figure 10.

Run the script. The file in the directory E:/wwwroot/avatar/BEIJI has been successfully renamed
Figure 10

11. Run the script. Error: iconv(): Detected an illegal character in input string, as shown in Figure 11.
Translation: iconv(): Detected an illegal character in input string

Run the script. Error: iconv(): Detected an illegal character in input string
Figure 11

12. Print the file name and find that the illegal character should be 吴䶮, as shown in Figure 12.

Print the file name and find that the illegal character should be 吴䶮
Figure 12

13. Change GB2312 to GB18030 (if changed to GBK, it still throws an error; the GB18030 character set includes the GBK character set and CJK Ext-A), as shown in Figure 13.

Change GB2312 to GB18030 (if changed to GBK, it still throws an error; the GB18030 character set includes the GBK character set and CJK Ext-A)
Figure 13

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系