2

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.

sybear
  • 7,837
  • 1
  • 22
  • 38
  • Looks like you are passing UTF-8 where Windows expects input in a non-UTF-8 ANSI-codepage. Could also be that in your code you somewhere get UTF-8 but expect a single-byte encoding. How about Writing your proposed filename to a file and checking? – Deduplicator Apr 14 '14 at 11:11
  • Well, I tried `iconv("UTF-8", "Windows-1252", $string);` which gives `jйrфme.jpg`, so what encoding Windows really expects? – sybear Apr 14 '14 at 11:17
  • Could you try running your program using the MS Tool [AppLocale](http://en.wikipedia.org/wiki/AppLocale), setting the codepage to UTF-8? – Deduplicator Apr 14 '14 at 11:30
  • I would like to avoid installing any additional stuff, since there should be some solution? Files are saved everywhere, unicode characters are widely used. – sybear Apr 14 '14 at 11:32
  • It looks like it's saving many of the characters as `cyrillic`; are you russian or something? either of these should work `iconv("UTF-8", "ISO-8859-1", $string);`, `utf8_decode($string);` – l'L'l Apr 14 '14 at 11:49
  • Possible duplicate of [how to iterate over non-English file names in PHP](http://stackoverflow.com/questions/2947941/how-to-iterate-over-non-english-file-names-in-php). I'm afraid it's just not possible. – Álvaro González Apr 14 '14 at 14:42

1 Answers1

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
Community
  • 1
  • 1