使用PHP向Windows写入一个文件名中包含日文字符的文件


Use PHP to write a file to Windows that contains Japanese characters in the filename

我想在文件名中使用日文字符将文件保存到Windows。

PHP文件以UTF-8编码保存

<?php
$oldfile = "test.txt";
$newfile = "日本語.txt";
copy($oldfile,$newfile);
?>

文件复制,但在Windows中显示为

æ——¥æœ¬eªž. txt

如何保存为

?

我已经结束了使用php-wfio扩展从https://github.com/kenjiuno/php-wfio

将php_wfio.dll放入php'ext文件夹并启用扩展名后,我用wfio://前缀文件名(两者都需要前缀,否则您会得到Cannot rename a file across wrapper types错误)

我的测试代码最终看起来像
<?php
$oldfile = "wfio://test.txt";
$newfile = "wfio://日本語.txt";
copy($oldfile,$newfile);
?>

文件在Windows中被保存为我想要的

从PHP 7.1开始,我会链接到这个答案https://stackoverflow.com/a/38466772/3358424。不幸的是,大多数建议都是无效的,它们被列在力求成为唯一正确答案的答案中。像"只需对文件名进行urlencode"或"FS期望iso-8859-1"等都是非常错误的假设,误导了人们。这可能是幸运的,但只对美国或几乎西方的代码页有效,但在其他方面是错误的。PHP 7.1 + default_charset=UTF-8是你想要的。对于早期的PHP版本,wfio或ext/com_dotnet的包装器可能确实很有帮助。

谢谢。