UTF-8 Filenames in PHP Filesystem Functions
Issue:
Creating directory using mkdir with UTF-8 characters results in incorrect display in Windows Explorer due to character encoding issues.
Question:
How can I create directory with UTF-8 characters using filesystem functions in PHP?
Solution:
Preferred Approach: URL Encoding
Urlencode the string intended as the filename. All characters returned by urlencode are valid in filenames on various platforms (NTFS/HFS/UNIX). To retrieve the original filename in UTF-8, simply urldecode the encoded string.
Caveats:
Alternative (but Less Ideal) Solutions:
String Encoding for Windows:
If using Windows, the PHP filesystem wrapper expects ISO-8859-1 strings. This approach has two options:
Caveats for Alternative Approaches:
Recommendation:
Normalize UTF-8 and consider transliteration for filenames to avoid potential issues and ensure consistent and platform-independent handling of file and directory names.
The above is the detailed content of How to Create Directories with UTF-8 Filenames Using PHP Filesystem Functions?. For more information, please follow other related articles on the PHP Chinese website!