SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post Fix recursive function on a directory listing

    Good day to you,
    Here i'm still working on my list of files pushed to an array.

    I'm now working on recursively display the list, when it read the files and it find a directory, it should go through and list the files and folders.. so on..

    I know my code is wrong, this is why I come to you, I want to learn.

    My code do the thing but only does it once.

    My problem : My code don't read recursively, it only go to the 2nd level of foler if any.

    How could I improve this ?



    PHP Code:

    <?php

    $directory 
    "Art/";
    function 
    dirList ($directory)
    {

        
    //create 2 arrays - one for folders and one for files
       
    $folders = array();
       
    $files = array();

        
    // create a handler for the directory
        
    $handler opendir($directory);

        
    // keep going until all files in directory have been read
    while (false !== ($file readdir($handler))) {  

            
    // if $file isn't this directory or its parent,
            // add it to the results array
            
    if ($file != '.' && $file != '..')
            
            
    // If file is directory, mark it in bold.
           
    if(is_dir($directory.$file)) { 
            
    array_push($folders,$file);
            
            
    // Else not styled
            
    }else{
            
    array_push($files,$file);
            
        }
        }


        
    // tidy up: close the handler
        
    closedir($handler);

        foreach(
    $folders as $folder) {
          echo 
    "<strong>".$folder."</strong><br />";
            
    echo 
    "<div>";
    dirList($directory."$folder.");

    echo 
    "</div><br/><br/>";
        
        
        }

        foreach(
    $files as $file) {
          echo 
    $file."<br />";
        }
        

    }

    dirList($directory);

    ?>
    The big picture is a menu of files and folder.
    Each folder result should be placed in a div.
    If there is a folder create a new div within the other and list the files in....

    Thanks !

  2. #2
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Manual is your friend. See code examples on the page:
    http://php.net/manual/en/class.recur...ryiterator.php
    I'm creating trouble-free Apache, PHP, MySQL installer, WITSuite,
    and use it to setup my development environment.
    Demo, support, contact. Questions?

  3. #3
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Peuplarchie:

    Your code is not recursive. Recursive means it calls itself and nowhere inside the function

    PHP Code:
     dirList ($directory) {} 
    is there a call to dirList ($directory). To make the function recursive it would look like this:

    PHP Code:
    function dirList ($directory
    {
        
    open $directory;
        while (
    file or folder) {
            
    read file or folder
            
    if (is directory) {
                
    dirList ($nextDirectory
            } else {
                
    process file
            
    }
        }
        return 
    results;

    Denny Schlesinger
    web services

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    done this so many times...
    PHP Code:
    function rglob $path )
    {
        
    $path preg_replace'~[\\\\/]+~''/'realpath$path ) . '/' );
        if ( !
    $path || !file_exists$path ) ) {
            
    trigger_error'Path not found!'E_USER_WARNING );
            return 
    false;
        }

        
    $ret   = array();
        
    $files glob$path '*' );
        foreach ( 
    $files as $file ) {
            if ( 
    is_dir$file ) ) {
                
    $retbasename$file ) ] = call_user_func__FUNCTION__$file );
                continue;
            }

            
    $ret[] = $file;
        }

        return 
    $ret;
    }

    print_rrglob'C:\Temp' ) ); 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •