更新(2017 年 7 月 13 日)
尽管文档似乎没有提及,但 PHP 7.0 及更高版本终于在 Windows 上开箱即用地支持 Unicode 文件名。 PHP 的文件系统 API 接受并返回文件名default_charset
,即UTF-8
默认情况下。
请参阅此处的错误修复:https://github.com/php/php-src/commit/3d3f11ede4cc7c83d64cc5edaae7c29ce9c6986f https://github.com/php/php-src/commit/3d3f11ede4cc7c83d64cc5edaae7c29ce9c6986f
更新(2015 年 1 月 29 日)
如果您有权访问 PHP 扩展目录,则可以尝试安装php-wfio.dll
at https://github.com/kenjiuno/php-wfio https://github.com/kenjiuno/php-wfio,并通过以下方式引用文件wfio://
协议。
file_get_contents("wfio://你好.xml");
原答案
Windows 上的 PHP 使用旧版“ANSI API”专门用于本地文件访问,这意味着 PHP 使用系统区域设置而不是统一码。
To access files whose filenames contain Unicode, you must convert the filename to the specified encoding for the current System Locale. If the filename contains characters that are not representable in the specified encoding, you're out of luck (Update: See section above for a solution). scandir
will return gibberish for these files and passing the string back in fopen
and equivalents will fail.
要找到正确使用的编码,您可以通过调用获取系统区域设置<?=setlocale(LC_TYPE,0)?>
,并查找代码页标识符(后面的数字.
)在 MSDN 文章中https://msdn.microsoft.com/en-us/library/dd317756(VS.85).aspx https://msdn.microsoft.com/en-us/library/dd317756(VS.85).aspx.
例如,如果函数返回Chinese (Traditional)_HKG.950
,这意味着 950 代码页正在使用,文件名应转换为 big-5 编码。在这种情况下,如果您的文件以 UTF-8 格式保存(最好没有 BOM),您的代码必须如下所示:
$fname = iconv('UTF-8','big-5',"你好.xml");
file_get_contents($fname);
如果直接保存为Big-5则如下:
$fname = "你好.xml";
file_get_contents($fname);