Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Monday, December 21, 2015

How to recursively iterate through files in PHP?

How to recursively iterate through files in PHP?


I have set up a basic script that is posting an array of paths to find template files inside them; currently it's only searching two levels deep and I'm having some troubles getting my head around the logic for an extensive loop to iterate all child directories until the length is 0.

So if I have a structure like this:

./components  ./components/template.html  ./components/template2.html  ./components/side/template.html  ./components/side/template2.html  ./components/side/second/template.html  ./components/side/second/template2.html  ./components/side/second/third/template.html  ./components/side/second/third/template2.html  

It's only searching up to the "side" directory for .html files when ideally I want it to check all child directories and the passed directory for .html files. Here is my working code so far:

  

Answer by Peter for How to recursively iterate through files in PHP?


PHP has the perfect solution for you built in. http://php.net/manual/en/class.directoryiterator.php

An example:

$it = new RecursiveDirectoryIterator("/components");  foreach(new RecursiveIteratorIterator($it) as $file) {      if ($file->getExtension() == 'html') {          echo $file;      }  }  

Answer by Adam for How to recursively iterate through files in PHP?


PHP 5 introduces iterators to iterate quickly through many elements.

You can use RecursiveDirectoryIterator to iterate recursively through directories.

You can use RecursiveIteratorIterator on the result to have a flatten view of the result.

You can use RegexIterator on the result to filter based on a regular expression.

 $directory_iterator = new RecursiveDirectoryIterator('.');   $iterator       = new RecursiveIteratorIterator($directory_iterator);   $regex_iterator = new RegexIterator($iterator, '/\.php$/');   $regex_iterator->setFlags(RegexIterator::USE_KEY);   foreach ($regex_iterator as $file) {      echo $file->getPathname() . PHP_EOL;   }  

With iterators, there are lot of ways to do the same thing, you can also use a FilterIterator (see the example on the page)

For instance, if you want to select the files modified this week, you can use the following:

  $directory_iterator = new RecursiveDirectoryIterator('.');    $iterator       = new RecursiveIteratorIterator($directory_iterator);      class CustomFilterIterator extends FilterIterator {         function accept() {              $current=$this->getInnerIterator()->current();              return ($current->isFile()&&$current->getMTime()>time()-3600*24*7);         }    }      $filter_iterator=new CustomFilterIterator($iterator);    foreach ($filter_iterator as $file) {       echo $file->getPathname() . PHP_EOL;    }  

Answer by Alpesh Panchal for How to recursively iterate through files in PHP?


Here is working code which scans given directory $dir and all of its sub directories to any levels and return list of html files and folders containing them.

  

Answer by Monty for How to recursively iterate through files in PHP?


Universal File search in folder, sub-folder :

function dirToArray($dir,$file_name) {     $result = array();     $cdir = scandir($dir);     foreach ($cdir as $key => $value)     {        if (!in_array($value,array(".","..")))        {           if (is_dir($dir . DIRECTORY_SEPARATOR . $value))           {              $result[$value] = dirToArray($dir . DIRECTORY_SEPARATOR . $value,$file_name);           }           else           {              if($value == $file_name){                  $result[] = $value;               }           }        }     }     return $result;  }    define('ROOT', dirname(__FILE__));  $file_name = 'template.html';  $tree = dirToArray(ROOT,$file_name);  echo "
".print_r($tree,1)."
";

OUTPUT :

Array  (      [components] => Array          (              [side] => Array                  (                      [second] => Array                          (                              [0] => template.html                              [third] => Array                                  (                                      [0] => template.html                                  )                            )                        [0] => template.html                  )                [0] => template.html          )        [0] => template.html  )  

Answer by Monty for How to recursively iterate through files in PHP?


Alternative of my previous answer :

$dir = 'components';  function getFiles($dir, &$results = array(), $filename = 'template.html'){      $files = scandir($dir);      foreach($files as $key => $value){          $path = realpath($dir.'/'.$value);          if(!is_dir($path)) {              if($value == $filename)                           $results[] = $path;                   } else if($value != "." && $value != "..") {              getFiles($path, $results);          }      }      return $results;  }  echo '
'.print_r(getFiles($dir), 1).'
';

OUTPUT :

Array  (      [0] => /web/practise/php/others/test7/components/side/second/template.html      [1] => /web/practise/php/others/test7/components/side/second/third/template.html      [2] => /web/practise/php/others/test7/components/side/template.html      [3] => /web/practise/php/others/test7/components/template.html  )  

Answer by trongd for How to recursively iterate through files in PHP?


Another solution is using Finder component from Symfony. It's tested and proven code. Take a look at it here: http://symfony.com/doc/current/components/finder.html


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.