简体   繁体   English

如何显示目录和所有子目录中的文件?

[英]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/ .我的类别是images/中的子目录。 When user doesn't choose a category then photos are stored in images/ .当用户不选择类别时,照片将存储在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这是我的 php 代码的片段

 $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您应该使用 RecursiveDirectoryIterator,请参阅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();

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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