php - How to use ::setMaxDepth for RecursiveIteratorIterator? -
i'm using script specify max depth recursive function. don't know how use it, error. how should use ::setmaxdepth here?
public function countfiles($path) { global $file_count; $depth=0; $ite=new recursivedirectoryiterator($path); $file_count=0; foreach (new recursiveiteratoriterator($ite) $filename=>$cur) : $file_count++; $files[] = $filename; endforeach; return $file_count; }
you need call setmaxdepth() on instance of recursiveiteratoriterator. difficult when construct recursiveiteratoriterator within foreach statement. instead, use variable hold it.
$files = new recursiveiteratoriterator($ite); $files->setmaxdepth($depth); foreach ($files $filename => $cur) { $file_count++; $files_list[] = $filename; } however note use of loop not required here (unless code other things have removed in example above). can count of files iterator_count().
function countfiles($path) { $depth = 1; $ite = new recursivedirectoryiterator($path, recursivedirectoryiterator::skip_dots); $files = new recursiveiteratoriterator($ite); $files->setmaxdepth($depth); return iterator_count($files); }
Comments
Post a Comment