Search code examples
phpdirectoryglob

PHP glob function with directories using special characters


Having directory named "Łęć" and using glob like this:

$dirs = glob( FILES . '/general/*' );

Gives me the result of:

...
(string) "../pliki/general/Logo"
(string) "../pliki/general/���"
(string) "../pliki/general/Maski"
...

And this ��� is the directory named Łęć

I totally can't figure it out how to make it work, so I can have folders with special characters and the glob() to work with it properly

                    $dirs = glob( FILES . '/general/q/*' );
                    foreach($dirs as &$dir)
                    {
                        $dir = bin2hex($dir);
                    }
                    dd($dirs);

This code above globs where Łęć folder is and bin2hex it's name returns: 2e2e2f706c696b692f67656e6572616c2f712fa3eae6 and the folder name alone without the path is a3eae6


Solution

  • a3eae6 is the hexadecimal representation of the string of unknown encoding returned for "Łęć". The string returned by glob() can write in PHP-Notation as "\xa3\xea\xe6". The conversion of this character string with an encoding unknown to us into UTF-8 must then result "Łęć".

    Through trial and error, I found that the "ISO-8859-2" encoding satisfies this condition:

    $strCode = "\xa3\xea\xe6";
    $name = mb_convert_encoding($strCode,"UTF-8","ISO-8859-2");
    var_dump($name === "Łęć"); //bool(true)
    

    The strings that glob returns must all be converted with mb_convert_encoding:

    $fullNameUTF8 = mb_convert_encoding($strFromGlob,"UTF-8","ISO-8859-2");
    

    This procedure is not certain. It's better to know the exact encoding used by the file system you are accessing.