Switch $path
with your path, and this should work. The SPL contains all the classes required for recursive directory traversal, there is no need to roll your own.
<?php
$path = '.';
$result = array('files' => array(), 'directories' => array());
$DirectoryIterator = new RecursiveDirectoryIterator($path);
$IteratorIterator = new RecursiveIteratorIterator($DirectoryIterator, RecursiveIteratorIterator::SELF_FIRST);
foreach ($IteratorIterator as $file) {
$path = $file->getRealPath();
if ($file->isDir()) {
$result['directories'][] = $path;
} elseif ($file->isFile()) {
$result['files'][] = $path;
}
}
print_r($result);
For more information, consult the PHP documentation: RecursiveDirectoryIterator, RecursiveIteratorIterator & SplFileInfo are great starting points!