I need something to read all files in a directory and all files in child directories and any directories below this.

Could someone provide a snippet for me.

Thanks :)

A hint would be to use the scandir and isdir PHP method to write a recursive method...

Post what you manage to write, and I'll happily help.

<?php
function recurseDir($dir)
{
      if(is_dir($dir))
      {
        if($dh = opendir($dir))
        {
            while(($file = readdir($dh)) !== false)
            {
                if($file != '.' && $file != '..')
                {
                    if(!is_dir($dir . $file))
                    {
                        if(is_dir($file))
                        {
                            echo "Directory->".$dir . $file."<br/>";
                        }
                        else
                        {
                            echo "File->".$dir . $file."<br/>";
                        }
                    }
                    else
                    {
                        if(is_dir($file))
                        {
                            echo "Directory->".$dir . $file."<br/>";
                        }
                        else
                        {
                            echo "File->".$dir . $file."<br/>";
                        }
                    }
                }
            }
        }
        closedir($dh);
      }
}

recurseDir(".");

?>

Works for me - have you looked at the DirectoryIterator? New(ish) function for php

http://uk3.php.net/directoryiterator

Nice examples here:

http://www.phpro.org/tutorials/Introduction-to-SPL-DirectoryIterator.html#1

Try this:

$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('.')); 

  foreach($files as $file) { 

     echo $file . "<br />";
}

AND techno info here:

http://www.php.net/~helly/php/ext/spl/directorytreeiterator_8inc-source.html


It's truly beautiful.

Try this

$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('.')); 

  foreach($files as $file) { 

     echo $file . "<br />";
	}
This article has been dead for over six months. Start a new discussion instead.