PHP list all files in directory

arrays, directory, file, php

Solution

foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator('.')) as $filename)
{
    // filter out "." and ".."
    if ($filename->isDir()) continue;

    echo "$filename\n";
}

PHP documentation:

- RecursiveDirectoryIterator

- RecursiveIteratorIterator

Problem

Possible Duplicate: Get the hierarchy of a directory with PHP Getting the names of all files in a directory with PHP I have seen functions to list all file in a directory but how can I list all the files in sub-directories too, so it returns an array like? ``` $files = files("foldername"); ``` So `$files` is something similar to ``` array("file.jpg", "blah.word", "name.fileext") ```

Original source

Related problems