PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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....
$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;
}
Last edited by mr_man on Mon Nov 21, 2011 6:03 am, edited 2 times in total.
//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
if(is_dir($path."/".$file)){
$tree_array[] = "$path/$file<br>";
}
Nope, that still gives the same result only showing the 1st level directories.....If I echo the array with each iteration I can see that it appears to be resetting the array somewhat. You'll see it shows the subdirectories of the includes directory and then in the next iteration it only shows the includes and the subdirectories are gone
$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"))
{
echo "<pre>";
print_r($tree_array);
echo "</pre>";
$tree_array[] = "$path/$file";
echo "$path/$file<br>";
// Re-call this same function but on a new directory.
getDirectory("$path/$file", ($level+1), $tree_array);
}
else
{
//echo "$spaces $file<br />";
// Just print out the filename
}
}
}
// Close the directory handle
closedir( $dh );
return $tree_array;
}