Home >Backend Development >PHP Tutorial >How Can I Handle UTF-8 Filenames in PHP's Filesystem Functions?
Handling UTF-8 Filenames in PHP's Filesystem Functions
When creating folders containing UTF-8 characters using PHP's mkdir function, you may encounter display issues in Windows Explorer due to encoding incompatibilities.
Solution: URL Encode Filenames
To resolve this issue, use the urlencode function to convert the desired folder name to a URL-safe format before passing it to mkdir:
$dir_name = urlencode("Depósito"); mkdir($dir_name);
This ensures that all characters in the folder name are encoded into valid filenames for all operating systems. To retrieve the original UTF-8 filename, use urldecode.
Alternative Solutions (with Caveats)
While URL encoding is the recommended solution, there are less attractive alternatives:
Convert to ISO-8859-1 (Windows Only)
On Windows, you can work with UTF-8 filenames but be aware that non-ASCII characters will appear incorrectly outside of PHP. To address this, use utf8_decode to convert filenames to ISO-8859-1 before using them in filesystem functions.
Restrict to ISO-8859-1 Characters
Alternatively, limit filenames to characters representable in ISO-8859-1. Use utf8_decode and utf8_encode to convert filenames between UTF-8 and ISO-8859-1.
However, these alternatives come with caveats:
The above is the detailed content of How Can I Handle UTF-8 Filenames in PHP's Filesystem Functions?. For more information, please follow other related articles on the PHP Chinese website!