-1

i have a big question about this trick, i have this script where is going in folder and taking all images from folders and sub folders, what i want is to display Title of parent of each folder so bassically if in my foreach is displaying from a folder 3 images and folder name is House 301 to have above the name of folder and under all 3 images and so on... because i have a structure like this ParentFolder>House 301, House 302, House 303, and in each folder have images. Here is my php:

            <?php
            function recursiveGlob($pattern)
            {
                $subPatterns = explode('/**/', $pattern);

                // Get sub dirs
                $dirs = glob(array_shift($subPatterns) . '/*', GLOB_ONLYDIR);

                // Get files in the current dir
                $files = glob($pattern);

                foreach ($dirs as $dir) {
                    $subDirList = recursiveGlob($dir . '/**/' . implode('/**/', $subPatterns));

                    $files = array_merge($files, $subDirList);
                }

                return $files;
            }?>

            <?php 

            $oras = $row['name'];
            $files = recursiveGlob("wp-content/themes/xxx/poze_lucrari/".$oras."/**/*.jpg");
            echo '<div class="imagini">';
            for ($i=0; $i<count($files); $i++)
            {
            echo '<div class="grid">';
            if(isset($files[$i])){
                $num = $files[$i];
                echo '<div class="grid-item"><a data-lightbox="example-set" href="https://www.xxx.ro/'.$num.'" data-title="Incalzire Electrica in pardoseala '.$oras.'"><img class="img-res" src="https://www.xxx.ro/'.$num.'" alt="Incalzire Electrica in pardoseala '.$oras.'" /></a></div>';
                echo $root;
                $num1 = str_replace($root.'Images/', '', $num);
                if (strpos($num1,'.png') == true) 
                {
                    $text = str_replace('.png', '', $num1);
                }
                if (strpos($num1,'.jpeg') == true) 
                {
                    $text = str_replace('.jpeg', '', $num1);
                }
                if (strpos($num1,'.jpg') == true) 
                {
                    $text = str_replace('.jpg', '', $num1);
                }
                if (strpos($num1,'.tiff') == true) 
                {
                    $text = str_replace('.tiff', '', $num1);
                }
                if (strpos($num1,'.gif') == true) 
                {
                    $text = str_replace('.gif', '', $num1);
                }

            }
            echo '</div>';
            }
            echo '</div>';
        ?>

I have multiple folders with cities and inside of each city i have anothers folders which they have inside images, i want automatically to enter on all city folder and display Name of folder and under images what have inside of him and next...etc for every folder.

Edward
  • 23
  • 5
  • This might help you https://stackoverflow.com/a/660736/1483629 – Vinay Patil Aug 21 '19 at 11:07
  • But how i can use that function to help me? i mean i want to display parent of folder Name and what is inside of that parent folder images just to display images – Edward Aug 21 '19 at 11:33
  • Can you add expected output in your question – Vinay Patil Aug 21 '19 at 15:16
  • In my server i have structure of folders like this: poze_lucrari(main folder)> Bucuresti(sub directory)> Home1(sub directory of folder from Bucuresti)> image1,image2,image3 Home2(sub directory of folder from Bucuresti)> image1,image2,image3 And here is how i want to display it as HTML https://i.stack.imgur.com/tRMTx.jpg – Edward Aug 22 '19 at 06:36

1 Answers1

0

This might help you. getDirContents will return all the folders with images.

function getDirContents($dir){
    $files = scandir($dir);

    foreach($files as $key => $value){
        $path = realpath($dir.DIRECTORY_SEPARATOR.$value);
        if(!is_dir($path)) {
            $results[] = $value;
        } else if($value != "." && $value != "..") {
            $results[$value] = getDirContents($path);
        }
    }
    return $results;
}

I have just provided the iteration of all directories, you need to add your div structure according to your need.

$directories = getDirContents('sub-directory'); // specify your sub-directory here as a parameter

if (!empty($directories)) {
    foreach ($directories as $directory => $images) {
        // this will be the parent folder of images
        echo $directory;
        if (!empty($images)) {
            foreach ($images as $image) {
                // add img tag here with valid url
                echo $image;
            }
        }
    }
}
Vinay Patil
  • 736
  • 6
  • 19