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?
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.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, "|");
In case you want to use
directoryIterator
Following function is a re-implementation of @Shef answer with
directoryIterator
You can also try this:
Have a look at glob() or the recursive directory iterator.
It will use to make menu bar in directory format
Here is A simple function with
scandir
&array_filter
that do the job. filter needed files using regex. I removed.
..
and hidden files like.htaccess
, you can also customise output using<ul>
and colors and also customise errors in case no scan or empty dirs!.