Please give me a solution for listing all the folders,subfolders,files in a directory using php. My folder structure is like this:
Main Dir Dir1 SubDir1 File1 File2 SubDir2 File3 File4 Dir2 SubDir3 File5 File6 SubDir4 File7 File8
I want to get the list of all the files inside each folder.
Is there any shell script command in php?
PHP using scandir() to find folders in a directory The scandir function is an inbuilt function that returns an array of files and directories of a specific directory. It lists the files and directories present inside the path specified by the user.
Substitute dir /A:D. /B /S > FolderList. txt to produce a list of all folders and all subfolders of the directory. WARNING: This can take a while if you have a large directory.
The scandir() function in PHP is an inbuilt function that is used to return an array of files and directories of the specified directory. The scandir() function lists the files and directories which are present inside a specified path.
function listFolderFiles($dir){ $ffs = scandir($dir); unset($ffs[array_search('.', $ffs, true)]); unset($ffs[array_search('..', $ffs, true)]); // prevent empty ordered elements if (count($ffs) < 1) return; echo '<ol>'; foreach($ffs as $ff){ echo '<li>'.$ff; if(is_dir($dir.'/'.$ff)) listFolderFiles($dir.'/'.$ff); echo '</li>'; } echo '</ol>'; } listFolderFiles('Main Dir');
A very simple way to show folder structure makes use of RecursiveTreeIterator
class (PHP 5 >= 5.3.0, PHP 7) and generates an ASCII graphic tree.
$it = new RecursiveTreeIterator(new RecursiveDirectoryIterator("/path/to/dir", RecursiveDirectoryIterator::SKIP_DOTS)); foreach($it as $path) { echo $path."<br>"; }
http://php.net/manual/en/class.recursivetreeiterator.php
There is also some control over the ASCII representation of the tree by changing the prefixes using RecursiveTreeIterator::setPrefixPart
, for example $it->setPrefixPart(RecursiveTreeIterator::PREFIX_LEFT, "|");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With