Posted By

iloveitaly on 06/24/09


Tagged

php list recursive files directory glob walk


Versions (?)

Recursive Glob


 / Published in: PHP
 

URL: http://www.php.net/manual/en/function.glob.php

To get all files in a directory:

$fileList = rglob("*", GLOB_MARK, '/path/to/dir');

foreach($fileList as $index => $file) { if($file[strlen($file) - 1] != "/") { // then it is a file } }

  1. rglob($pattern, $flags = 0, $path = '') {
  2. if (!$path && ($dir = dirname($pattern)) != '.') {
  3. if ($dir == '\\' || $dir == '/') $dir = '';
  4. return rglob(basename($pattern), $flags, $dir . '/');
  5. }
  6. $paths = glob($path . '*', GLOB_ONLYDIR | GLOB_NOSORT);
  7. $files = glob($path . $pattern, $flags);
  8. foreach ($paths as $p) $files = array_merge($files, rglob($pattern, $flags, $p . '/'));
  9. return $files;
  10. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: iroybot on August 17, 2009

This has to fail - you are calling rglob() several times inside itself. (line 4 and again in line 8)

Posted By: esoomllub on September 22, 2010

Nice use of recursion.

iroybot... if you don't really understand recursion, you are likely not to want to touch this.

Posted By: hm2k on January 7, 2011

Latest version is found here

You need to login to post a comment.