开发者

How to return folders that do not contain a certain file?

I'd like to filter out folders that do not contain poster.* (JPG/PNG/GIF) OR folder.* (JPG/PNG/开发者_Go百科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!


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

?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜