Directory tree array won't show nested directories [FIXED!]
Posted: Sun Nov 20, 2011 10:40 pm
I'm trying to hack this script to return an array with a full directory listing that includes nested directories. I'm trying to make it return only the directories with no file names. As I have it now it only returns the 1st level directories. I'm sure that I'm missing something really simple but I'm hoping somebody on here would be able to point it out to me....
Code: Select all
$tree_array = getDirectory(".");
echo "<pre>";
print_r($tree_array);
echo "</pre>";
function getDirectory( $path = '.', $level = 0,$tree_array = array())
{
// Directories to ignore
$ignore = array( 'cgi-bin', '.', '..', 'plesk-stat' );
// Open the directory to the handle $dh
$dh = @opendir( $path );
// Loop through the directory
while( false !== ( $file = readdir( $dh ) ) )
{
// Check that this file is not to be ignored
if( !in_array( $file, $ignore ) )
{
// Just to add spacing to the list, to better
// show the directory tree.
$spaces = str_repeat( ' ', ( $level * 4 ) );
// Its a directory, so we need to keep reading down...
if( is_dir("$path/$file"))
{
if(!in_array("$path/$file",$tree_array))
{
$tree_array[] = "$path/$file";
}
//here if I just echo out the path it displays it the
//way that I'm trying to get it to do with the array
echo "$path/$file<br>";
// Re-call this same function but on a new directory.
getDirectory("$path/$file", ($level+1), $tree_array);
}
}
}
// Close the directory handle
closedir( $dh );
return $tree_array;
}