I want to save some data in files with file names which would have unicode characters in it (like chinese, cyrillic, arabic, ...) with PHP's file_put_contents() functions. I don't want to encode them separately with something like urlencode() because no human would be able to read the file names if they contained only non-latin chars. The 3 biggest OSes Windows, MacOS / OS X and Linux support UTF-8 or UTF-16 chars in file names and can display them without problems but it seems that it's not that easy as just calling something like that:
我想用文件名保存一些数据,文件名中包含unicode字符(如中文,西里尔文,阿拉伯语,...)和PHP的file_put_contents()函数。我不想用urlencode()之类的东西单独编码它们,因为如果它们只包含非拉丁字符,那么没有人能够读取文件名。 Windows操作系统,MacOS / OS X和Linux这三个最大的操作系统支持文件名中的UTF-8或UTF-16字符,并且可以毫无问题地显示它们,但似乎只是调用类似的东西并不容易:
file_put_contents(__DIR__ . DIRECTORY_SEPARATOR . "こんにちは.txt", "");
On Windows 7 (German localization) the file is stored as:
在Windows 7(德语本地化)上,文件存储为:
ã“ã‚“ã«ã¡ã¯.txt
The PHP file itself is saved in UTF-8 encoding. Is there an uniform way to save files with unicode in the file name on this 3 systems?
PHP文件本身以UTF-8编码保存。在这3个系统上是否有统一的方法来保存文件名中带有unicode的文件?
2 个解决方案
#1
As answered here It's a known Windows problem (glob() can't find file names with multibyte characters on Windows?).
这里回答这是一个已知的Windows问题(glob()在Windows上找不到包含多字节字符的文件名?)。
You could try URLify to alleviate the problem only on Windows, files should have readable names then...
您可以尝试URLify来缓解仅在Windows上的问题,文件应该具有可读的名称然后...
#2
file_put_contents(__DIR___.iconv("UTF-8", "cp1251", $filename), $data);
- works perfect!
file_put_contents(__ DIR ___。iconv(“UTF-8”,“cp1251”,$ filename),$ data); - 完美的作品!
#1
As answered here It's a known Windows problem (glob() can't find file names with multibyte characters on Windows?).
这里回答这是一个已知的Windows问题(glob()在Windows上找不到包含多字节字符的文件名?)。
You could try URLify to alleviate the problem only on Windows, files should have readable names then...
您可以尝试URLify来缓解仅在Windows上的问题,文件应该具有可读的名称然后...
#2
file_put_contents(__DIR___.iconv("UTF-8", "cp1251", $filename), $data);
- works perfect!
file_put_contents(__ DIR ___。iconv(“UTF-8”,“cp1251”,$ filename),$ data); - 完美的作品!