0

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

cherryBom
  • 53
  • 1
  • 6
  • 1
    Either create a `recursive` function to drill down into all folders and sub-folders or, preferably, use a combination of [recursiveIterator](https://www.php.net/manual/en/class.recursiveiterator.php) and [recursiveDirectoryIterator](https://www.php.net/manual/en/class.recursivedirectoryiterator.php) – Professor Abronsius Jun 06 '20 at 21:10

1 Answers1

1

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

        }
Maarten Veerman
  • 1,546
  • 1
  • 8
  • 10
  • thank you so much, it helped me to get all files but when I want to display them I only get some blank fields like they cannot be opened – cherryBom Jun 06 '20 at 21:30
  • Do you mean the images don't show up in your browser? Are the images located in a folder which is accessible by your webserver? The fact that php can see contents of a folder doesn't mean your webserver allows access to that folder. – Maarten Veerman Jun 06 '20 at 21:44
  • yes they don't show up. the problem appeared now, when I displayed only photos from main directory everything was okay. I gave permissions to server so folders are accessible – cherryBom Jun 07 '20 at 07:58
  • In your browser, check the development pane, look in the network tab, load the page. You should see errors, load failures. What error codes do you see? – Maarten Veerman Jun 07 '20 at 09:02
  • so that's what I get: failed to load resource: the server responded with a status of 404 (not found) – cherryBom Jun 07 '20 at 09:22
  • I also noticed that my photos have type: text/html instead of jpg – cherryBom Jun 07 '20 at 09:58
  • If it's a 404, you need to verify the URL of the images. Is the URL really correct, pointing to the file in the subdirectory? – Maarten Veerman Jun 07 '20 at 16:10
  • I think so, according to the code you sent my `$folder="./images"` and then I try to show files: `` `` – cherryBom Jun 07 '20 at 16:18
  • Well, the `getRealPath` function returns the full path to the file on the subdirectory (https://www.php.net/manual/en/splfileinfo.getrealpath.php). You need to modify that path to a path matching an URL. For example `/path/images/sub/image.jpg` should become `/images/sub/image.jpg`. So as a start strip the path to the images folder from the realPath. – Maarten Veerman Jun 07 '20 at 16:46
  • ah thank you! I replaced the path to `images` with '/' and it worked for files but also for subdirectories which are shown as blank squares even though I used `!is_dir` – cherryBom Jun 07 '20 at 17:17
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/215478/discussion-between-maarten-veerman-and-sylwia). – Maarten Veerman Jun 07 '20 at 18:36