If file extension exists

I’m not sure which way to go or even how to write what I need. Should I use ‘opendir’ or ‘glob’, I want to open a certain file when another file doesn’t exist. I want to search a main directory and certain 2nd level sub directories, not all the 2nd level sub directories though, and not any 3rd level sub directories. Basically if “file_extn A = true” then continue search, if “file_extn A = false” then echo “file_extn B”. I’ve uploaded a sample image to try and give a better idea. The blue cells indicate the directories I want to search, the yellow cells are directories I want to exclude from the search and the green cell is the file I want to echo, if anybody can help it would be greatly appreciated.

Try with DirectoryIterator().
Here’s an example, not sure if it works, but it is an ideea to start to create your script.
The ideea is to make an array with directories in which to search, traverse the array and get info about each directory.
A tutorial about DirectoryIterator(), with more examples, you can find on: DirectoryIterator to get file and directory info.

$dirtosearch = array('dir', 'dir/a', 'dir/b');
$nrdir = count($dirtosearch);
for($i=0; $i<$nrdir; $i++) {
$objDI = new DirectoryIterator($dirtosearch[$i]);
  foreach($objDI as $fileinfo) {
    // if it's a file
    if($fileinfo->isFile()) {
      if($fileinfo->getExtension() == 'file_extn A') {
        echo 'file_extn B';
        break 2;

Cheers MarPlo I’ll give it a try and let you know, thanx for the link also, mate

Sorry I made an error in my original post. I want to post or get 'file_extn B’s actual file, not echo the extn. Otherwise this code may work. Sorry for the misunderstanding.
As 'file_extn B’s actual file creates 'file extn A’s actual file (after user input), I was also wondering if it can be deleted after say a 24 hour period by implementing extra code in the code that creates it. Cheers