0

I have a directory called images. Lets say the path is

somepath.com/images/

Within my images directory is a number of folders. So lets say I have

somepath.com/images/folder1
somepath.com/images/folder2

Within each of these folders are a number of images. So lets say I have

somepath.com/images/folder1/img1.png
somepath.com/images/folder1/img2.png
somepath.com/images/folder2/img1.png
somepath.com/images/folder2/img2.png

I am trying to build up an array containing all of this data. I would expect the array to look something like this

array:2 [▼
  "directories" => array:1 [▼
    "Name" => "folder1"
  ]
  "files" => array:14 [▼
    0 => array:1 [▼
      "Name" => "img1.png"
    ]
    2 => array:1 [▼
      "Name" => "img2.png"
    ]
  ]
]
array:2 [▼
  "directories" => array:1 [▼
    "Name" => "folder2"
  ]
  "files" => array:14 [▼
    0 => array:1 [▼
      "Name" => "img1.png"
    ]
    2 => array:1 [▼
      "Name" => "img2.png"
    ]
  ]
]

This way, I can see there is a folder1 and inside of this is img1 and img2. To produce this array, I am currently scanning the base directory to get the folder names. I then loop the folders in order to get the file names. This is what I currently have

$data = array();
$directories = scandir("somepath.com/images/");

foreach($directories as $directory){
    if($directory=='.' or $directory=='..' ){
        continue;
    } else{
        $data["directories"] = array(
            "Name" => $directory
        );

        $dirFiles = scandir("somepath.com/images/" . $directory);
        foreach($dirFiles as $file){
            if($file=='.' or $file=='..' ){
                continue;
            } else {
                $data["files"][] = array(
                    "Name" => $file
                );
            }
        }
    }
}

The problem I am having at the moment is the first directory data being overwritten. Additionally, the array I am left with seems to have all 4 images for folder2. How can I alter what I have in order to produce the desired output?

Thanks

katie hudson
  • 2,765
  • 13
  • 50
  • 93

2 Answers2

2

While the structure is slightly different by using the array index as the folder name you can use this code: Deep recursive array of directory structure in PHP which would produce an array like:

array:2 [
  "folder1" => array:2 [
    "img1.png",
    "img2.png"
  ],
  "folder2" => array:2 [
    "img1.png",
    "img2.png"
  ]
]

The code will also traverse multiple sub-directories, so given the structure in the first block it would also create a result like the second block:

somepath.com/images/folder1/img1.png
somepath.com/images/folder1/img2.png
somepath.com/images/folder1/subfolder1/img1.png
somepath.com/images/folder1/subfolder1/img2.png
somepath.com/images/folder2/img1.png
somepath.com/images/folder2/img2.png
somepath.com/images/folder2/subfolder1/img1.png
somepath.com/images/folder2/subfolder1/img2.png

-- yields:

array:2 [
  "folder1" => array:3 [
    "subfolder1" => array:2 [
      "img1.png",
      "img2.png"
    ]
    "img1.png",
    "img2.png"
  ],
  "folder2" => array:3 [
    "subfolder1" => array:2 [
      "img1.png",
      "img2.png"
    ]
    "img1.png",
    "img2.png"
  ]
]
Community
  • 1
  • 1
Jim
  • 3,210
  • 2
  • 17
  • 23
1

You may want to check out the FolderScanner Class Found here. Perhaps it does the Trick for you. You can just include it in your Project and work with it like so:

    <?php
        require_once "FolderScanner.php";

        // EXTRACT ALL IMAGE FILES IN "IMAGES" DIRECTORY 
        // REPLACE THE PATH ACCORDING TO YOUR PROJECT'S STRUCTURE  
        $pathToImagesDirectory  = "somepath.com/images/"; 
        $imgFilesInImagesDir    = FolderScanner::scan_folder_for_image_files($pathToImagesDirectory); 

        // $THE $imgFilesInImagesDir CONTAINS THE STRUCTURED DATA...
        // var_dump($imgFilesInImagesDir);
Poiz
  • 7,611
  • 2
  • 15
  • 17