Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP detecting filesystem encoding/saving files with non-latin filenames

I need to save files with non-latin filenames on a filesytem, using PHP.

I want to make this work cross-platform. How do I know what encoding I can use to write the file? I understand many modern filesystems are UTF-8 based (is this correct?), but I doubt Windows XP is (for instance).

So, is there a robust detection mechanism?

like image 599
Evert Avatar asked Mar 26 '10 11:03

Evert


1 Answers

Not an answer to your question, but if you don't need to do extensive operations on filesystem level (like searching, sorting...), there is a nice cross-platform workaround for the issue outlined in this SO question: URLEncode()ing file names.

Hörensägen.txt 

gets turned into

H%c3%b6rens%c3%a4gen.txt

which should be safe to use in any filesystem and is able to map any UTF-8 character.

I find this much preferable to trying to "natively" deal with the host OS's capabilities, which is guaranteed to be complicated and error-prone (in addition to operating system differences, I'm sure the various filesystem formats - FAT16, FAT32, NTFS, extFS versions 1/2/3.... bring their own set of rules to be aware of.)

like image 126
Pekka Avatar answered Nov 15 '22 00:11

Pekka