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:
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3