如何返回不包含某个文件的文件夹?

时间:2011-05-22 20:39:53

标签: php regex opendir

我想过滤掉不包含海报的文件夹。*(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 个答案:

答案 0 :(得分:1)

在这里,你希望它有所帮助:

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

?>