php scan_it以排除以下划线开头的目录

时间:2016-06-03 04:58:16

标签: php arrays

我有以下代码,它扫描指定目录中的jpgs:

<?php
$scan_it = new RecursiveDirectoryIterator("images/" . $this->item->extraFields->PropertYID->value . "");

foreach(new RecursiveIteratorIterator($scan_it) as $file) {
if (strtolower(substr($file, -4)) == ".jpg" &&
strtolower(substr($file, -4)) == ".jpg") {

echo '<div><img src="'.$file .'" alt="" />'."</div>";
}
}
?>

我只想在此扫描中排除任何名称以下划线(_)开头的文件夹。

澄清目录结构是否如下:

/images/interior/

/images/exterior/

/images/_room1/

/images/room2/

/images/room3/

将包括除_room1之外的所有目录。

非常感谢提前!

1 个答案:

答案 0 :(得分:0)

您可以在文件夹名称

中搜索“_”
$directory = "images/' . $this->item->extraFields->PropertYID->value '";
        $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory), RecursiveIteratorIterator::CHILD_FIRST);
    foreach($files as $name => $file){
        if(is_dir($file)){ //Edited
             $pos = strpos($file, '_');
        }
         if($pos === false){
            if (strtolower(substr($file, -4)) == ".jpg" &&
            strtolower(substr($file, -4)) == ".jpg") {

            echo '<div><img src="'.$file .'" alt="" />'."</div>";
            }
          }
    }

如果你想删除只有第一个字符为'_'的目录,如果它在中间就可以,那么你可以这样做

    $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory), RecursiveIteratorIterator::CHILD_FIRST);
foreach($files as $name => $file){
    if(is_dir($file)){
         $pos = strpos($file, '_');
    }
     if(($pos === false) || $pos>0){
        if (strtolower(substr($file, -4)) == ".jpg" &&
        strtolower(substr($file, -4)) == ".jpg") {

        echo '<div><img src="'.$file .'" alt="" />'."</div>";
        }
      }
}