简体   繁体   中英

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

You should use a RecursiveDirectoryIterator, see https://www.php.net/manual/en/class.recursivedirectoryiterator.php

You could then do something like:

$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();

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM