Save filename with unicode chars

2019-05-31 01:36发布

问题:

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