how to list what is inside a directory using PHP?

Dear Experts,
I use PHP 7 and Linux server
I want to list all the folders and files inside a directory

I want to list it like this:

firstfolder
      file1.jpg
      file2.jpg
      file3.jpg
secondefolder
      filename.jpg
      filename2.jpg

How can I do that with PHP?

below code works perfectly fine. However it only brings me one folder, I want to see nested folders and what is inside..

$handle = opendir(dirname(realpath('myfolder')).'/myfolder/1234/');
        while($file = readdir($handle)){
            if($file !== '.' && $file !== '..'){
                echo '<img src="myfolder/1234/'.$file.'" border="0" />';
            }
 }

Open in new window


thank you
LVL 1
BRMarketingAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
How many levels? Sounds like you want a recursive solution - but you can end up with a lot of output.
0
BRMarketingAuthor Commented:
I have two levels actually.
I have some folders, and there are some pictures in it...

firstfolder
      file1.jpg
      file2.jpg
      file3.jpg
secondefolder
      filename.jpg
      filename2.jpg
0
David FavorLinux/LXD/WordPress/Hosting SavantCommented:
The opendir() function only returns one level of directory contents.

http://php.net/manual/en/class.recursivedirectoryiterator.php is likely what you're looking for.
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BRMarketingAuthor Commented:
thank you all
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.