-1

I'd like to filter out folders that do not contain poster.* (JPG/PNG/GIF) OR folder.* (JPG/PNG/GIF). I created the following code but I'm a bit clueless on how to do this efficiently:

<?php
$dir = "/share/test/";
if ($handle = opendir($dir)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." &&
            $file != ".." &&
            $file != ".DS_Store" &&
            $file != "_Incoming" &&
            is_dir($dir.$file) &&
            !file_exists($dir.$file."/poster.*") ){

                echo "$file\n";

        }
    }
    closedir($handle);
}
?>

Thanks!

FLX
  • 4,634
  • 14
  • 47
  • 60
  • http://stackoverflow.com/search?q=folder+php+image – Gordon May 22 '11 at 21:17
  • possible duplicate of [Regex help: Match any image file beginning with an underscore](http://stackoverflow.com/questions/2139627/regex-help-match-any-image-file-beginning-with-an-underscore) – Gordon May 22 '11 at 21:18
  • possible duplicate of [Read files in a folder](http://stackoverflow.com/questions/3563863/read-files-in-folder/3564311#3564311) – Gordon May 22 '11 at 21:22
  • Sorry to be the bearer of bad news, but I’m afraid that `opendir`, `readdir`, and `closedir` only operate on directories, not “folders” – whatever *those* are. – tchrist May 22 '11 at 22:26
  • @tchrist lol potato/potato - referer/referrer - I tend to call folders "folders" when there in directory's saves calling them sub-directories – Lawrence Cherone May 22 '11 at 22:35
  • @tchrist: in computer speak folders and directories are the same thing. I *nix it used to be called a directory, then Windows came and called it a folder, now both are used interchangeably with a slight bias towards originating OS nomenclature. – Andrew Savinykh May 22 '11 at 22:42

1 Answers1

1

here ya go, hope it helps:

<?php 
/**
 * Recursive function that will check all folders in the 
 * requested directory for keywords [$lookfor] [Any extention]
 * true,false on returning notfounds 
 *
 * @param $path to folder
 * @param $lookfor word in filename or folder
 * @param $showNotfounds [true|false]
 * @return echo'ed out
 */
function lookfor($path,$lookfor,$showNotfounds=false){
    if(file_exists($path) && is_readable($path)){}else{die('Error reading folder ./'.$path.'');}
    if ($handle = @opendir($path)) {
        while ($file = readdir($handle)){

            if ($file=='.' || $file=='..'){}else{
                if (is_dir($path."/".$file)){
                    //Recursive
                    if(stristr($file, $lookfor) === FALSE) {
                        //[folder]missing
                        echo ($showNotfounds==true) ? '<font color="red"><b>'.$path.'/'.$file.'</b></font>: Not a '.$lookfor.'<br/>': '';
                    }else{
                        //[folder]exists
                        echo '<font color="green"><b>'.$path.'/'.$file.'</b></font>: Is a '.$lookfor.'<br/>';
                    }
                    lookfor($path."/".$file,$lookfor,$showNotfounds);
                }else{
                    if(stristr($file, $lookfor) === FALSE) {
                        //[file]missing
                        echo ($showNotfounds==true) ? '<font color="red"><b>'.$path.'/'.$file.'</b></font>: Not a '.$lookfor.'<br/>': '';
                    }else{
                        //[file]exists
                        echo '<font color="green"><b>'.$path.'/'.$file.'</b></font>: Is a '.$lookfor.'<br/>';
                    }
                }
            }
        }
        closedir($handle);
    }
}

//example usage
lookfor('.','folder.',true);
echo '<hr>';
lookfor('.','poster.',true);
echo '<hr>';
lookfor('.','poster.jpg',false);

?>
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106