2011-05-22 126 views
-1

我想过滤不包含海报的文件夹*(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); 
} 
?> 

谢谢!

+0

http://stackoverflow.com/search?q=folder+php+image的[正则表达式帮助 – Gordon 2011-05-22 21:17:23

+0

可能重复:匹配任何图像文件与开始下划线](http://stackoverflow.com/questions/2139627/regex-help-match-any-image-file-beginning-with-an-underscore) – Gordon 2011-05-22 21:18:43

+0

可能重复的[读取文件夹中的文件](http:///stackoverflow.com/questions/3563863/read-files-in-folder/3564311#3564311) – Gordon 2011-05-22 21:22:28

回答

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

?> 
+0

我比以往任何时候都可以要求劳伦斯,比你还要多! – FLX 2011-05-23 22:16:36

相关问题