kate_hudson kate_hudson - 4 months ago 18
PHP Question

Looping directories and saving data

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

Jim Jim
Answer

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 something a result like the 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:2 [
    "subfolder1" => array:2 [
      "img1.png",
      "img2.png"
    ]
    "img1.png",
    "img2.png"
  ],
  "folder2" => array:2 [
    "subfolder1" => array:2 [
      "img1.png",
      "img2.png"
    ]
    "img1.png",
    "img2.png"
  ]
]