tony9099
tony9099

Reputation: 4717

get the filenames of the images inside folders using php

I have the below folder structure.

main_folder subfolder1 image001.jpg image002.jpg image003.jpg subfolder2 image001.jpg image002.jpg image002.jpg image004.jpg image005.jpg subfolder3 image001.jpg image002.jpg image002.jpg image004.jpg

I want to get the pathname and filename of each image. I know I can use pathinfo($File); to get it, but I can't get into the folders and the sub-folders. I tried the following code to make this possible, but it is not working.

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('/main_folder/');

Upvotes: 2

Views: 41

Answers (1)

Sujal Patel
Sujal Patel

Reputation: 2532

Try glob to easy to get all filename inside the folder

Something like:

foreach(glob('./images/*.*') as $filename){
     echo $filename;
 }

Upvotes: 2

Related Questions