简体   繁体   English

使用PHP将文件写入Windows的文件名中包含日语字符

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

I want to save a file to Windows using Japanese characters in the filename. 我想使用文件名中的日语字符将文件保存到Windows。

The PHP file is saved with UTF-8 encoding PHP文件以UTF-8编码保存

<?php
$oldfile = "test.txt";
$newfile = "日本語.txt";

copy($oldfile,$newfile);
?>

The file copies, but appears in Windows as 该文件已复制,但在Windows中显示为

日本語.txt 日本語.txt

How do I make it save as 如何将其另存为

日本語.txt 日本语.txt

?

I have ended up using the php-wfio extension from https://github.com/kenjiuno/php-wfio 我最终使用了来自https://github.com/kenjiuno/php-wfio的php-wfio扩展名

After putting php_wfio.dll into php\\ext folder and enabling the extension, I prefixed the filenames with wfio:// (both need to be prefixed or you get a Cannot rename a file across wrapper types error) 将php_wfio.dll放入php \\ ext文件夹并启用扩展名后,我给文件名加上wfio://前缀(都需要加上前缀,否则会出现“ Cannot rename a file across wrapper types错误)

My test code ends up looking like 我的测试代码最终看起来像

<?php
$oldfile = "wfio://test.txt";
$newfile = "wfio://日本語.txt";

copy($oldfile,$newfile);
?>

and the file gets saved in Windows as 日本語.txt which is what I was looking for 然后将文件保存为Windows我想要的Windows.txt文件

Starting with PHP 7.1, i would link you to this answer https://stackoverflow.com/a/38466772/3358424 . 从PHP 7.1开始,我会将您链接到此答案https://stackoverflow.com/a/38466772/3358424 Unfortunately, the most of the recommendations are not valid, that are listed in the answer that strives to be the only correct one. 不幸的是,大多数建议都是无效的,这些建议在试图成为唯一正确答案的答案中列出。 Like "just urlencode the filename" or "FS expects iso-8859-1", etc. are terribly wrong assumptions that misinform people. 像“只是对文件名进行urlencode编码”或“ FS期望iso-8859-1”之类的信息,是完全错误的假设,会误导人们。 That can work by luck but are only valid for US or almost western codepages, but are otherwise just wrong. 运气可以,但是只对美国或几乎西方的代码页有效,否则就是错误的。 PHP 7.1 + default_charset=UTF-8 is what you want. PHP 7.1 + default_charset=UTF-8是您想要的。 With earlier PHP versions, wfio or wrappers to ext/com_dotnet might be indeed helpful. 在早期的PHP版本中,wfio或ext / com_dotnet的包装可能确实有用。

Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM