简体   繁体   English

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

[英]php scan_it to exclude directories starting with an underscore

I have the following code, which scans the specified directory for jpgs: 我有以下代码,它扫描指定的目录中的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>";
}
}
?>

Id simply like to exclude from this scan any folders which name start with an underscore (_). 我只想在此扫描中排除任何名称以下划线(_)开头的文件夹。

To clarify if the directory structure was the following: 澄清目录结构是否如下:

/images/interior/

/images/exterior/

/images/_room1/

/images/room2/

/images/room3/

All directories except _room1 would be included. 将包括除_room1之外的所有目录。

Many thanks in advance! 提前谢谢了!

You can search for '_' in the folder name 您可以在文件夹名称中搜索“_”

$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>";
            }
          }
    }

If you want to remove directory with only the first character as '_' and its ok if its in middle then you can do this 如果你想删除只有第一个字符为'_'的目录,如果它在中间,那就ok了,那么你可以这样做

    $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>";
        }
      }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM