Spaces to display levels in dir structure list ?

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

Spaces to display levels in dir structure list ?

Post by Peuplarchie »

Good day to you all,

working on listing a directory the way I want, I'm trying that for each level of file there is a number of spaces in front of the file and/or folder of that level. EX :

Folder 1
--Folder 2
--Image 1
--Image 2
--Image 3
Image 1
Image 2

How can I add that spacing on each level ?

Here is my code :

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>  <a href=\"javascript&#058;show('".$folder."');\">Show</a>- <a href=\"javascript&#058;hide('".$folder."');\">Hide</a><br />";
        
echo "<div id=\"".$folder."\">";
dirList($directory.$folder.'/');
 
echo "<br/><br/></div>";
    
    
    }
 
    foreach($files as $file) {
      echo $file."<br />";
    }
    
 
}
 
dirList($directory);
 
?>
 
 

Thanks !
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Re: Spaces to display levels in dir structure list ?

Post by n00b Saibot »

A simple recursive function + &nbsp;/&#160;/css:padding-left
Post Reply