Save filename with unicode chars

妖精的绣舞 提交于 2019-12-08 03:36:34

问题


I have searched all over the Internet and SO, still no luck in the following:

I would like to know, how to properly save a file using file_put_contents when filename has some unicode characters. (Windows 7 as OS)

$string = "jérôme.jpg" ; //UTF-8 string
file_put_contents("images/" . $string, "stuff");

Resuts in a file:

jГ©rГґme.jpg

Tried all possible combinations of such functions as iconv and mb_convert_encoding with all possible encodings, converting source file into different encodings as well. All proper headers are set, browser recognises UTF-8 properly.

However, I can successfully copy-paste and create a file with such a name in explorer's GUI, but how to make it via PHP?

The last hardcore solution was to urlencode the string and save file.


回答1:


This might be late but i just found a solution to close this hurting issue for me as well. Forget about iconv and multibyte solutions; the problem is on Windows! (in the link you'll find all it's beauty about this.)

After numerous attempts and ways to solve this, i met with URLify and decided that best way to cope with unicode-in-filenames is to transliterate them before writing to file.

Example of transliterating a filename before saving it:

$filename = "Αρχείο.php";   // greek name for 'file'
echo URLify::filter($filename,128,"",TRUE);
// output:   arxeio.php


来源:https://stackoverflow.com/questions/23058449/save-filename-with-unicode-chars

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!