Как вернуть папки, которые не содержат определенный файл? - PullRequest
0 голосов
/ 23 мая 2011

Я хочу отфильтровать папки, в которых нет постера. * (JPG / PNG / GIF) ИЛИ. * (JPG / PNG / GIF).Я создал следующий код, но я немного не понимаю, как это сделать эффективно:

<?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);
}
?>

Спасибо!

1 Ответ

1 голос
/ 23 мая 2011

вот, пожалуйста, надеюсь, это поможет:

<?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);

?>
...