简体   繁体   中英

php scan_it to exclude directories starting with an underscore

I have the following code, which scans the specified directory for 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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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