List all files folders and subfolders inside a directory using php

Click to rate this tutorial!
[Total: 0 Average: 0]

If you want to get the list of all the files and subfolders inside a folder or directory, Then here are the best scripts to do so.

We are providing you two different scripst that list the files and folders, You can grab them according to your requirement.

Script 1

function listFolderFiles($dir){
    $ffs = scandir($dir);
    echo '<ol>';
    foreach($ffs as $ff){
        if($ff != '.' && $ff != '..'){
            echo '<li>'.$ff;
            if(is_dir($dir.'/'.$ff)) listFolderFiles($dir.'/'.$ff);
            echo '</li>';
        }
    }
    echo '</ol>';
}
listFolderFiles('C:\xampp\htdocs');

Script 2

$path = realpath('C:\xampp\htdocs');
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)) as $filename)
{
        echo "$filename<br>";
}

The path ” C:\xampp\htdocs ” we have given is just for test purpose and you are free to modify it accordingly.

Click to rate this tutorial!
[Total: 0 Average: 0]

One reply on “List all files folders and subfolders inside a directory using php”

I like the valuable information you provide in your articles.
I’ll bookmark your weblog and check again here regularly. I am
quite sure I’ll learn lots of new stuff right here! Best of luck for the next!

Leave a Reply

Your email address will not be published.