PHP: イテレーターでディレクトリーを走査してファイル名でループする


関数

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/// $directory あるいは、配列の要素に RecursiveDirectoryIterator を指定した場合、
/// そのオブジェクトに対して、引数の$flagsは無視。
/// @param string/array/RecursiveDirectoryIterator $directory ディレクトリーパス
/// @param callable $callback function ($current, $key, $iterator);
/// @param int $flags <http://php.net/manual/ja/class.filesystemiterator.php#filesystemiterator.constants>
function createRecursiveDirectoryIterator(
    $directory,
    callable $callback = null,
    $flags = FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO
) {
    if ($directory instanceof RecursiveDirectoryIterator) {
        $iterator = $directory;
    } elseif (is_string($directory)) {
        $iterator = new RecursiveDirectoryIterator($directory, $flags);
    } elseif (is_array($directory)) {
        $iterator = new AppendIterator;
        foreach ($directory as $value) {
            $rdi = createRecursiveDirectoryIterator($value, $callback, $flags);
            if ($rdi === false) {
                return false;
            }
            $iterator->append($rdi);
        }
        return $iterator;
    } else {
        return false;
    }
    if (is_callable($callback)) {
        $iterator = new RecursiveCallbackFilterIterator($iterator, $callback);
    }
    return new RecursiveIteratorIterator($iterator);
}
/// @param string/array $directory  ディレクトリーパス
/// @param null/string/array $extension 拡張子
/// @param array $exclude 除外パス
/// @param int $flags <http://php.net/manual/ja/class.filesystemiterator.php#filesystemiterator.constants>
function createFilesIterator(
    $directory,
    $extension = null,
    $exclude = null,
    $flags = FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO
) {
    if (!is_null($extension) || !empty($exclude)) {
        $callback = function ($current, $key, $iterator) use ($exclude, $extension) {
            if ($iterator->isFile()) {
                return (is_null($extension) || in_array($iterator->getExtension(), (array)$extension));
            }
            return ($iterator->hasChildren() && !in_array($iterator->getFilename(), (array)$exclude));
        };
    } else {
        $callback = null;
    }
    return createRecursiveDirectoryIterator($directory, $callback, $flags | FilesystemIterator::SKIP_DOTS);
}
/// @param string/array $directory  ディレクトリーパス
/// @param null/string/array/callable $mime mime情報。例:['image/gif', 'image/jpeg', 'image/png']
/// @param array $exclude 除外パス
/// @param int $flags <http://php.net/manual/ja/class.filesystemiterator.php#filesystemiterator.constants>
function createImageFilesIterator(
    $directory,
    $mime = null,
    $exclude = null,
    $flags = FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO
) {
    if (is_callable($mines)) {
        $callback = $mime;
    } elseif (is_array($mines)) {
        $callback = function ($current, $key, $iterator) use ($mime) {
            $info = @getimagesize($iterator->getPathname());
            return (($info !== false) && in_array($info['mime'], $mime));
        };
    } else {
        $callback = function ($current, $key, $iterator) {
            $info = @getimagesize($iterator->getPathname());
            return (($info !== false) && (strpos($info['mime'], 'image/') === 0));
        };
    }
    return createRecursiveDirectoryIterator(
        $directory,
        function ($current, $key, $iterator) use ($exclude, $callback) {
            if ($iterator->isFile()) {
                return $callback($current, $key, $iterator);
            }
            return ($iterator->hasChildren() && !in_array($iterator->getFilename(), (array)$exclude));
        },
        $flags |= FilesystemIterator::SKIP_DOTS
    );
}

使い方

<?php
require_once 'createFilesIterator.function.php';

foreach (createFilesIterator('../path/to/dir', ['php', 'inc'], ['vendor']) as $fileinfo) {
    echo $fileinfo->getPathname().PHP_EOL;
}