3

How can I search a specific directory and its subdirectories for a specific file?

What I've tried are the following two functions:

function getImageDirectory($ipaPath) {
    $oDirectory = new RecursiveDirectoryIterator($ipaPath);
    $oIterator = new RecursiveIteratorIterator($oDirectory);
    foreach($oIterator as $oFile) {
        if ($oFile->getFilename() == 'info.plist') {
           return $oFile->getPath();
        }
    }
}

EDIT: This one works, just as well as the answer bellow! My example above return the directory of the file you are looking for, in this case "info.plist".

EDIT2: Thanks for the down votes! If someone would have asked a similar question it would have shown when I wrote the title. But nothing for PHP. So if I search for the wrong thing, not using developer language it shouldn't be down voted! And now someone with my low php skills might find an answer to this question! Is stackoverflow only for PRO's or for anyone that needs help?

just_user
  • 11,769
  • 19
  • 90
  • 135
  • see [`glob`](http://php.net/manual/en/function.glob.php) – xkeshav Jul 17 '12 at 09:59
  • 3 dislikes? Is my question that horrible? – just_user Jul 17 '12 at 10:03
  • There are so many duplicates, talking about the same issue. Look here : http://stackoverflow.com/questions/2014474/php-read-sub-directories-and-loop-through-files-how-to – BMN Jul 17 '12 at 10:08
  • Thanks, I found an answer there. If someone would have asked a similar question it would have shown when I wrote the title. But nothing for PHP. So if I search for the wrong thing, not using developer language it shouldn't be down voted! And now someone with my low php skills might find an answer to this question! Is stackoverflow only for PRO's or for anyone that needs help? – just_user Jul 17 '12 at 10:23
  • I think the downvotes were for your initial question that showed nothing exept a "do me this please" look-a-like. – BMN Jul 17 '12 at 12:12

1 Answers1

0

As @diEcho suggested, use glob. Get file list like this:

<?php
foreach( glob( "*.*" ) as $filename ) {
  echo "$filename size " . filesize( $filename ) . "\n";
}
?>

After that, just go through the results. You might need a recursive function, to access subfolders though.

Peon
  • 7,902
  • 7
  • 59
  • 100