phpphotophoto-gallery

how to show files from directory and all subdirectories?


I'm working on my photo gallery when user can upload photos and also choose a category. My categories are subdirectories in images/. When user doesn't choose a category then photos are stored in images/. I want to display all photos (from all categories and other) in gallery but now I manage to show only these without category. Can someone help me? Here's fragment of my php code

 $fo=opendir("images");

        if ($dh = opendir("images")){

        $count = 1;

        while($file=readdir($fo)){


            if($file!="" && $file!="." && $file!=".."){
                        $image_path = "images/".$file;

                if(!is_dir($image_path)){
    ?>
                      <div class="gallery"> 

                <a href="<?= $image_path; ?>">
                <img src="<?= $image_path; ?>">

I tried to list all subdirectories

$directories = glob("images" . '/*' , GLOB_ONLYDIR);

but I don't know what to do next


Solution

  • Using RecursiveDirectoryIterator you could do something like this:

    $folder = 'path/to/your/folder';
    
    $files = new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator($folder, RecursiveDirectoryIterator::SKIP_DOTS),
        RecursiveIteratorIterator::CHILD_FIRST
    );
    
    foreach ($files as $fileinfo) {
        // fileinfo is an object, see documentation for info
        $path = $fileinfo->getRealPath();
    }