Fix recursive function on a directory listing

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!

Moderator: General Moderators

Post Reply
User avatar
Peuplarchie
Forum Contributor
Posts: 148
Joined: Sat Feb 04, 2006 10:49 pm

Fix recursive function on a directory listing

Post by Peuplarchie »

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 ?


Code: Select all

 
 
<?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 !
Post Reply