views:

296

answers:

4

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!

A: 

Use scandir()

Andrew
+4  A: 
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
Thanks! Did exactly what I needed.
Callum
+2  A: 
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)) as $f) {
    echo "$f \r\n";   
}
Tom Haigh
A: 

$dir = "/home/user/stuff/";
$scan = scandir($dir);

foreach ($scan as $output) {
            echo "$output";
}