file_exists() 和 file_get_contents() 在 PHP 中名为 output‹ÕÍÕ¥.txt 的文件上失败?

2021-12-27 00:00:00 windows utf-8 encoding filesystems php

file_exists()file_get_contents() 在名为 output‹ÕÍÕ¥.txt 的文件上失败(例如),虽然我知道它存在?

file_exists() and file_get_contents() fail on a file which is named output‹ÕÍÕ¥.txt (as an example), although I know it exists?

我猜它与文件名中的特殊字符有关吗?.

I'm guessing its got something to do with the special characters within the file name?.

所以想知道有什么解决方法吗?

So was wondering whats a workaround?

感谢所有回复.

仅供参考:

请注意,如果您想为什么不简单地更改文件名?- 我不能因为文件名是一般生成的,并且更改文件名将意味着使用 PHP 的文件函数(这似乎不允许文件名 args 中的特殊字符 - 除非我误解/误解了任何东西).

Please note, if your thinking why not simply change the file name? - I can't as the file name is generated generically, and to change the file name will be mean using PHP's file functions (which don't seem to allow special characters within the file name args - unless I've misinterpreted/misunderstood anything).

我在 Windows 上使用 PHP 5.2.

I'm using PHP 5.2 on Windows.

推荐答案

确保文件系统的编码与 PHP 代码中包含文件名的字符串的编码相同.

Ensure that the encoding of the file-system is the same as the encoding of the string that contains the file-name in your PHP code.

否则您正在测试是否存在错误的文件.

Otherwise you're testing for the wrong file to exist.

例如,如果您的文件系统不支持 UTF-8,但文件名以 UTF-8 编码.

For example, if your file-system does not support UTF-8 but the file-name is encoded in UTF-8.

所以你评论说文件名是 UTF-8 并且你使用的是 windows.

So you have commented that the filename is in UTF-8 and you're using windows.

这正是我回答的重点.正如此处所写:

That's exactly the point my answer was about. As written here:

Windows 用户的坏消息:Windows 文件系统(FAT、FAT32、NTFS)不支持 UTF-8.根据文件系统和本地化,文件名以 ISO-8859-1 或 CP437/CP850/(或任何适合您的语言的方式)编码.因此,如果您遵循本指南,则在尝试将文件名从数据库匹配到文件系统时可能会遇到麻烦.(虽然我从未尝试过.)

Bad news for Windows Users: Windows filesystems (FAT, FAT32, NTFS) are not UTF-8 aware. Filenames are encoded in ISO-8859-1 or CP437/CP850/( or whatever suits your language) depending on filesystem and localisation. So you will probably get in trouble trying to match filenames from database to filesystem if you follow this guide. (Although I've never tried it.)

Windows 解决方法:现代 Windows 版本在安装的共享上支持 UTF-8.共享您的媒体文件夹并将其安装为网络驱动器.

Windows workaround: Modern windows versions support UTF-8 on mounted shares. Make a share of your mediafolder and mount it as a network drive.

因此,要么进行共享,要么以实际适用于您的文件系统的编码方式对文件名进行编码.这不是 PHP 中的错误.您只是没有检查正确的文件名,仅此而已.

So either make a share or encode the filename in the encoding that actually works with your filesystem. This is not a bug in PHP. You're just no checking for the right file-name, that's all.

编辑 2:

假设您有 NTFS,它可能接受 UTF-16(这不是真正精确的 AFAIK,但您可以尝试一下),您可以将 UTF-8 文件名转换为 UTF-16名称:

Assuming you have NTFS which might accept UTF-16 (which is not really precise AFAIK, but you can give it a try), you could convert the UTF-8 filename into an UTF-16 fiĺename:

$name = mb_convert_encoding($name, 'UTF-16', 'UTF-8');

然后尝试 file_exists 是否有效.请记住,他在技术上可能不正确,而且您可能还有一个额外的编码问题,即使进行了这种重新编码,也无法正确使用.

And then try if file_exists works. Keep in mind that his might not be technically correct, and you might have an additional encoding issue which prevents you from correct use even with this re-encoding.

相关:NTFS 中的文件名存储为什么编码?

相关文章