I want to make script to search one file in more than one directory, for example I have structure of dir's:
-images
--2014
--2015
--2016
Now my PHP script should search FILE.jpg in one of 2014/2015/2016 (folders will be create dynamically, so I can't put list of folders in script (just folder 'images' will be always).
Best regards!
You can merely use
glob().In the precise case you describe it should look like this:
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:
It'll return all occurrences of
$targetfile anywhere in the given$context.Caution: it may be much time consuming if
contextis a big structure!So you might limit the search depth, like this:
In the other hand, if you plan to retrieve only a unique file, you might more simply stop as soon as it's found: