This will allow you to navigate all the directories in the path and will look for everything that the image has / has the extensions that you defined. Since you told the other user that you have a ziparchive part, I omitted this ...
<?php function traverse($path, $images = []) { $files = array_diff(scandir($path), ['.', '..']); foreach ($files as $file) { // check if the file is an image if (in_array(strtolower(pathinfo($file, PATHINFO_EXTENSION)), ['jpg', 'jpeg', 'png', 'gif'])) { $images[] = $file; } if (is_dir($path . '/' . $file)) { $images = traverse($path . '/' . $file, $images); } } return $images; } $images = traverse('/Users/kyle/Downloads');
You want to complete this process:
- Get all files in the current working directory
- If the file in CWD is an image, add it to the image array
- If the file in CWD is a directory, the trace function is recursively called and looks for images in the directory
- In the new CWD, find the images if the file is a directory registry, etc.
It is important to keep track of the current path so that you can call is_dir in the file. Also you want not to search for "." or "..", or you will never come across a basic recursive case / it will be infinite.
Also, this will not support the directory path for the image! If you want to do this, you must do $image[] = $path . '/' . $file; $image[] = $path . '/' . $file; . You might want to do this, and then get the entire contents of the file so that the function exits. I would not recommend sorting the contents in the $ image array, because it could use an absurd amount of memory.
source share