How to recursively iterate through files in PHP?

后端 未结 7 1825
走了就别回头了
走了就别回头了 2021-02-12 16:47

I have set up a basic script that is posting an array of paths to find template files inside them; currently it\'s only searching two levels deep and I\'m having some troubles g

7条回答
  •  天涯浪人
    2021-02-12 17:21

    PHP has the perfect solution for you built in.

    Example

    // Construct the iterator
    $it = new RecursiveDirectoryIterator("/components");
    
    // Loop through files
    foreach(new RecursiveIteratorIterator($it) as $file) {
        if ($file->getExtension() == 'html') {
            echo $file;
        }
    }
    

    Resources

    • DirectoryIterator - Manual

提交回复
热议问题