Get directory hierarchy with PHP

I try to find all files and folders in the 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 

Hope this makes sense!

+10
file directory filesystems php
Mar 19 '09 at 0:34
source share
9 answers
 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>"; } } } } 
+13
Mar 19 '09 at 1:14
source share
— -
 foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)) as $f) { echo "$f \r\n"; } 
+5
Mar 19 '09 at 11:28
source share

Working solution (change with the name of your folder)

 <?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"; } ?> 
+2
Mar 28 '13 at 10:54
source share
0
Mar 19 '09 at 0:39
source share
 $dir = "/home/user/stuff/"; $scan = scandir($dir); foreach ($scan as $output) { echo "$output" . "<br />"; } 
0
Mar 19 '09 at 15:34
source share
 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/>"; } } 
0
Dec 29 '15 at 12:40
source share

Besides the RecursiveDirectoryIterator solution, there is also a 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 ); } ); 
0
Mar 24 '15 at 16:33
source share

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

0
Apr 16 '15 at 14:46
source share

Find all files and folders in the 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) 
0
Nov 05 '15 at 16:50
source share



All Articles