Return to Snippet

Revision: 15623
at September 23, 2009 02:02 by DrewDouglass


Updated Code
<?php
$path = realpath('/etc');

$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
foreach($objects as $name => $object){
    echo "$name\n";
}
?>

Revision: 15622
at July 12, 2009 04:01 by DrewDouglass


Updated Code
<?php
//New version added, see the reply about halfway down, located in the original url.
//This functions is indeed much faster.
function get_dir_iterative($dir = '.', $exclude = array( 'cgi-bin', '.', '..' ))
{
    $exclude = array_flip($exclude);
    if(!is_dir($dir))
    {
        return;
    }
    
    $dh = opendir($dir);
    
    if(!$dh)
    {
        return;
    }

    $stack = array($dh);
    $level = 0;

    while(count($stack))
    {
        if(false !== ( $file = readdir( $stack[0] ) ) )
        {
            if(!isset($exclude[$file]))
            {
                print str_repeat('&nbsp;', $level * 4);
                if(is_dir($dir . '/' . $file))
                {
                    $dh = opendir($file . '/' . $dir);
                    if($dh)
                    {
                        print "<strong>$file</strong><br />\n";
                        array_unshift($stack, $dh);
                        ++$level;
                    }
                }
                else 
                {
                    
                    print "$file<br />\n";
                }
            }
        }
        else 
        {
            closedir(array_shift($stack));
            --$level;
        }
    }
} 
?>

<?php
//This was the original snippet added to snipplr
function getDirectory( $path = '.', $level = 0 ){

    $ignore = array( 'cgi-bin', '.', '..' );
    // Directories to ignore when listing output. Many hosts
    // will deny PHP access to the cgi-bin.

    $dh = @opendir( $path );
    // Open the directory to the handle $dh
    
    while( false !== ( $file = readdir( $dh ) ) ){
    // Loop through the directory
    
        if( !in_array( $file, $ignore ) ){
        // Check that this file is not to be ignored
            
            $spaces = str_repeat( '&nbsp;', ( $level * 4 ) );
            // Just to add spacing to the list, to better
            // show the directory tree.
            
            if( is_dir( "$path/$file" ) ){
            // Its a directory, so we need to keep reading down...
            
                echo "<strong>$spaces $file</strong><br />";
                getDirectory( "$path/$file", ($level+1) );
                // Re-call this same function but on a new directory.
                // this is what makes function recursive.
            
            } else {
            
                echo "$spaces $file<br />";
                // Just print out the filename
            
            }
        
        }
    
    }
    
    closedir( $dh );
    // Close the directory handle

}

?>

Revision: 15621
at July 12, 2009 03:41 by DrewDouglass


Initial Code
<?php

function getDirectory( $path = '.', $level = 0 ){

    $ignore = array( 'cgi-bin', '.', '..' );
    // Directories to ignore when listing output. Many hosts
    // will deny PHP access to the cgi-bin.

    $dh = @opendir( $path );
    // Open the directory to the handle $dh
    
    while( false !== ( $file = readdir( $dh ) ) ){
    // Loop through the directory
    
        if( !in_array( $file, $ignore ) ){
        // Check that this file is not to be ignored
            
            $spaces = str_repeat( '&nbsp;', ( $level * 4 ) );
            // Just to add spacing to the list, to better
            // show the directory tree.
            
            if( is_dir( "$path/$file" ) ){
            // Its a directory, so we need to keep reading down...
            
                echo "<strong>$spaces $file</strong><br />";
                getDirectory( "$path/$file", ($level+1) );
                // Re-call this same function but on a new directory.
                // this is what makes function recursive.
            
            } else {
            
                echo "$spaces $file<br />";
                // Just print out the filename
            
            }
        
        }
    
    }
    
    closedir( $dh );
    // Close the directory handle

}

?>

Initial URL
http://us2.php.net/manual/en/class.recursivedirectoryiterator.php

Initial Description
Takes advantage of the SPL. Much easier and maintainable than the previous revisions. Haven't done any benchmarks yet but I imagine it is much faster. "If you want to omit directories, remove the RecursiveIteratorIterator::SELF_FIRST part."

Initial Title
Recursive Directory Listing (Show full directory structure)

Initial Tags
php

Initial Language
PHP