0

I'm making module to get list of directories in specified path using PHP DirectoryIterator. There is example:

class GardenUtils
{
    public static function getPath()
    {
        return public_path() . "/images/garden/";
    }

    public static function getAlbumDirectories()
    {
        $directories = [];

        $directoryIterator = new DirectoryIterator(self::getPath());

        foreach ($directoryIterator as $directory) {
            if (!$directory->isDot() && $directory->isDir()) {
                $directories[] = FileUtil::normalizeFileName($directory->getFilename());
            }
        }

        return $directories;
    }
}

My problem is a result of scanning in the array are missing some of the directory. For example - 12 are placed in the directory, and fall into the array 10. If i remove $directory->isDir() condition, all files will be scanned. In addition, if i call $directory->getType(), script crashing with RuntimeException. All directories contains Russian letters and diacritics.

Example of name that scanned correctly: Аквилегия (Aquilégia)
Example of name, that cause problems: Хоста (Hósta) (h5-30)

That problem reproduced with PHP 5.6.12 on Windows 7 with Russian locale. PHP 5.5.25 on Linux system works correctly.

How can i solve it?

iRomul
  • 251
  • 1
  • 7
  • 18
  • I'm not sure you can solve this using a directory iterative unfortunately. May have to use glob and handle the multibyte conversions yourself. – Ohgodwhy Mar 27 '16 at 17:14
  • what will it say if you change `$directories[] = FileUtil::normalizeFileName($directory->getFilename());` to `$directories[] = $directory->getFilename();` ? – xmike Mar 27 '16 at 20:40
  • @xmike Unfortunately, it could not, because the error seems to occur somewhere deeper. So, as Ohgodwhy said, it is worth trying the method with [scandir](http://php.net/manual/en/function.scandir.php) or [globs](http://stackoverflow.com/questions/2524151/php-get-all-subdirectories-of-a-given-directory) – iRomul Mar 27 '16 at 21:47

0 Answers0