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