Page 1 of 1

Directory tree array won't show nested directories [FIXED!]

Posted: Sun Nov 20, 2011 10:40 pm
by mr_man
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( '&nbsp;', ( $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;
} 

Re: Directory tree array won't show nested directories

Posted: Mon Nov 21, 2011 4:33 am
by mr_man
This is what the inline echo statement outputs (which is displayed the way that I'm trying to get the array structure to be):

./ftp
./ftp/ftp_log
./images
./images/makethis
./images/animated_this
./images/animated
./ftp_src
./includes
./includes/functions
./includes/fish
./includes/classes
./includes/bla
./includes/templates
./ftp_log

and this is what the returned array is:
Array
(
[0] => ./ftp
[1] => ./images
[2] => ./ftp_src
[3] => ./includes
[4] => ./ftp_log
)

Re: Directory tree array won't show nested directories

Posted: Mon Nov 21, 2011 4:45 am
by maxx99
Just add:

Code: Select all

//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>"; 
}

Re: Directory tree array won't show nested directories

Posted: Mon Nov 21, 2011 5:27 am
by mr_man
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

Array
(
[0] => ./ftp
[1] => ./images
[2] => ./ftp_src
[3] => ./includes
[4] => ./includes/functions
[5] => ./includes/fish
[6] => ./includes/classes
[7] => ./includes/bla
)

Array
(
[0] => ./ftp
[1] => ./images
[2] => ./ftp_src
[3] => ./includes

Re: Directory tree array won't show nested directories

Posted: Mon Nov 21, 2011 5:30 am
by mr_man
This code functions and will give you the results i'm seeing if you want to copy and paste

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( '&nbsp;', ( $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;
} 

Re: Directory tree array won't show nested directories

Posted: Mon Nov 21, 2011 5:46 am
by maxx99
Sorry I didn't notice that youre not doing anything with

Code: Select all

getDirectory("$path/$file", ($level+1), $tree_array);
You need to store the result otherwise $tree_array as a local variable lives only as long as the function.

Re: Directory tree array won't show nested directories

Posted: Mon Nov 21, 2011 5:50 am
by mr_man
That's it....

Code: Select all

$tree_array = getDirectory("$path/$file", ($level+1), $tree_array); 
I knew it would be something really simple I was missing. Thank you!