You can merely use glob()
.
In the precise case you describe it should look like this:
$files = glob('path_to_images/*/FILE.jpg');
More generally, put as many /*
as needed between the known folder path and the searched file name to explore a given depth level.
Edit, extending the solution according to the OP's comment
If you don't know anything about the tree structure, you can do a deep multi-level search, like this:
function doGlob($target, $context) {
if ($dirs = glob($context . '/*', GLOB_ONLYDIR)) {
foreach ($dirs as $dir) {
$result = array_merge($result, doGlob($target, $dir));
}
}
return $result;
}
$files = doGlob('FILE.jpg', 'path_to_images');
It'll return all occurrences of $target
file anywhere in the given $context
.
Caution: it may be much time consuming if context
is a big structure!
So you might limit the search depth, like this:
function doGlob($target, $context, $max_depth = -1, $depth = 0) {
$result = glob($context . '/' . $target);
if ($depth > $max_depth) {
return $result;
}
if ($dirs = glob($context . '/*', GLOB_ONLYDIR)) {
foreach ($dirs as $dir) {
$result = array_merge($result, doGlob($target, $dir, $max_depth, $depth + 1));
}
}
return $result;
}
$files = doGlob('FILE.jpg', 'path_to_images', <max-depth>);
In the other hand, if you plan to retrieve only a unique file, you might more simply stop as soon as it's found:
function doGlob($target, $context) {
if ($result) {
return $result;
}
if ($dirs = glob($context . '/*', GLOB_ONLYDIR)) {
foreach ($dirs as $dir) {
$result = array_merge($result, doGlob($target, $dir));
}
}
return $result;
}
$files = doGlob('FILE.jpg', 'path_to_images');