Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecursiveDirectoryIterator Removing Dot folders

Tags:

php

Currently I have the code below what would be the best way for me to be able to set the flags in the code below so I can use ::SKIP_DOTS?

Code:

  $folder = 'images/banner_img';?>

    <?php foreach( new RecursiveIteratorIterator(new RecursiveDirectoryIterator($folder)) as $filename): ?>
      <?php $fileTypes = array("db"); 
            $fileType  = pathinfo($filename,PATHINFO_EXTENSION);

            if(!in_array(strtolower($fileType), $fileTypes)):?>


         <img src="<?php echo $filename; ?>" alt="" title="" data-thumb="<?php echo $filename;?>" />

       <?php endif;?>

    <?php endforeach;?>
like image 447
Jess McKenzie Avatar asked Jan 15 '15 02:01

Jess McKenzie


1 Answers

Setting RecursiveDirectoryIterator::SKIP_DOTS as an option (in the second parameter) to new RecursiveDirectoryIterator() should be sufficient to work with your current code.

With PHP 5.3+, you may alternatively declare the RecursiveDirectoryIterator as its own variable then call a method on that variable:

$iterator = new RecursiveDirectoryIterator($folder);
$iterator->setFlags(RecursiveDirectoryIterator::SKIP_DOTS);
// Still need to pass $iterator to a RecursiveIteratorIterator...

setFlags() is a method inherited from the FilesystemIterator base class.

But since you are already using the template formatting foreach: / endforeach; style, it makes sense to just do it in the nested declaration rather than using additional variables.

<!-- Instead, stick with your current iterator declaration and -->
<!-- add RecursiveDirectoryIterator::SKIP_DOTS as the 2nd param to its constructor -->
<?php foreach( new RecursiveIteratorIterator(new RecursiveDirectoryIterator($folder, RecursiveDirectoryIterator::SKIP_DOTS)) as $filename): ?>
<!-------------------------------------------------------------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -->
like image 193
Michael Berkowski Avatar answered Nov 06 '22 08:11

Michael Berkowski