Forked from yahyaahrika/gist:28dcca8aed469decebf05f4df9e891d7
Created
October 9, 2021 21:29
-
-
Save therbta/3c377e86a54f9caec1fd5da7eda27139 to your computer and use it in GitHub Desktop.
php how-to-create-unicode-filenames
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http://stackoverflow.com/questions/6467501/php-how-to-create-unicode-filenames | |
1 | |
down vote | |
Using the com_dotnet PHP extension, you can access Windows' Scripting.FileSystemObject, and then do everything you want with UTF-8 files/folders names. | |
I packaged this as a PHP stream wrapper, so it's very easy to use : | |
https://github.com/nicolas-grekas/Patchwork-UTF8/blob/lab-windows-fs/class/Patchwork/Utf8/WinFsStreamWrapper.php | |
First verify that the com_dotnet extension is enabled in your php.ini then enable the wrapper with: | |
stream_wrapper_register('win', 'Patchwork\Utf8\WinFsStreamWrapper'); | |
Finally, use the functions you're used to (mkdir, fopen, rename, etc.), but prefix your path with win:// | |
For example: | |
<?php | |
$dir_name = "Depósito"; | |
mkdir('win://' . $dir_name ); | |
?> | |
---------------------------------- | |
or me the code below works well on Win7/ntfs, Apache 2.2.21.0 & PHP 5.3.8.0: | |
<?php | |
// this source file is utf-8 encoded | |
$fileContent = "Content of my file which contains Turkish characters such as şığŞİĞ"; | |
$dirName = 'Dirname with utf-8 chars such as şığŞİĞ'; | |
$fileName = 'Filename with utf-8 chars such as şığŞİĞ'; | |
// converting encodings of names from utf-8 to iso-8859-9 (Turkish) | |
$encodedDirName = iconv("UTF-8", "ISO-8859-9//TRANSLIT", $dirName); | |
$encodedFileName = iconv("UTF-8", "ISO-8859-9//TRANSLIT", $fileName); | |
mkdir($encodedDirName); | |
file_put_contents("$encodedDirName/$encodedFileName.txt", $fileContent); | |
you can do same thing for opening files: | |
<?php | |
$fileName = "Filename with utf-8 chars such as şığ"; | |
$fileContent = file_get_contents(iconv("UTF-8", "ISO-8859-9//TRANSLIT", "$fileName.txt")); | |
print $fileContent; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment