8

I'm trying to find all the files and folders under a specified directory

For example I have /home/user/stuff

I want to return

/home/user/stuff/folder1/image1.jpg
/home/user/stuff/folder1/image2.jpg
/home/user/stuff/folder2/subfolder1/image1.jpg
/home/user/stuff/image1.jpg

Hopefully that makes sense!

Callum
  • 1,004
  • 2
  • 12
  • 18

8 Answers8

11
function dir_contents_recursive($dir) {
    // open handler for the directory
    $iter = new DirectoryIterator($dir);

    foreach( $iter as $item ) {
        // make sure you don't try to access the current dir or the parent
        if ($item != '.' && $item != '..') {
            if( $item->isDir() ) {
                // call the function on the folder
                dir_contents_recursive("$dir/$item");
            } else {
                // print files
                echo $dir . "/" .$item->getFilename() . "<br>";
            }
        }
    }
}
Steve Willard
  • 16,647
  • 4
  • 28
  • 26
5
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)) as $f) {
    echo "$f \r\n";   
}
Tom Haigh
  • 57,217
  • 21
  • 114
  • 142
2

The working solution (change with your folder name)

<?php
$path = realpath('yourfolder/subfolder');
## or use like this
## $path = '/home/user/stuff/folder1';

foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)) as $filename)
{
        echo "$filename\n";
}
?>
T.Todua
  • 53,146
  • 19
  • 236
  • 237
1

Find all the files and folders under a specified directory.

function getDirRecursive($dir, &$output = []) {
    $scandir = scandir($dir);

    foreach ($scandir as $a => $name) {
        $path = realpath($dir . DIRECTORY_SEPARATOR . $name);
        if (!is_dir($path)) {
            $output[] = $path;
        } else if ($name != "." && $name != "..") {
            getDirRecursive($path, $output);
            $output[] = $path;
        }
    }

    return $output;
}

var_dump(getDirRecursive('/home/user/stuff'));

Output (example) :

array (size=4)
  0 => string '/home/user/stuff/folder1/image1.jpg' (length=35)
  1 => string '/home/user/stuff/folder1/image2.jpg' (length=35)
  2 => string '/home/user/stuff/folder2/subfolder1/image1.jpg' (length=46)
  3 => string '/home/user/stuff/image1.jpg' (length=27)
user2226755
  • 12,494
  • 5
  • 50
  • 73
1
$dir = "/home/user/stuff/";
$scan = scandir($dir);

foreach ($scan as $output) {
    echo "$output" . "<br />";
}
Raj
  • 22,346
  • 14
  • 99
  • 142
0
listAllFiles( '../cooktail/' ); //send directory path to get the all files and floder of root dir

function listAllFiles( $strDir ) {

    $dir = new DirectoryIterator( $strDir );

    foreach( $dir as $fileinfo ) {
        if( $fileinfo == '.' || $fileinfo == '..' ) continue;

        if( $fileinfo->isDir() ) {
            listAllFiles( "$strDir/$fileinfo" );
        }

        echo $fileinfo->getFilename() . "<br/>";
    }
}
carlodurso
  • 2,886
  • 4
  • 24
  • 37
Somnath
  • 159
  • 1
  • 1
  • 7
0

Beside RecursiveDirectoryIterator solution there is also glob() solution:

// do some extra filtering here, if necessary
function recurse( $item ) {
    return is_dir( $item ) ? array_map( 'recurse', glob( "$item/*" ) ) : $item;
};

// array_walk_recursive: any key that holds an array will not be passed to the function. 
array_walk_recursive( ( recurse( 'home/user/stuff' ) ), function( $item ) { print_r( $item ); } );
Danijel
  • 12,408
  • 5
  • 38
  • 54
0

You can use the RecursiveDirectoryIterator or even the glob function. Alternatively, the scandir function will do the job.

Eclipse
  • 374
  • 2
  • 4
  • 17