Jump to content

Recommended Posts

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 :

 

 



<?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:show('".$folder."');\">Show</a>- <a href=\"javascript: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 !

pass a level along with the function and increment it recursively:

 

<?php
$directory = "./";
function dirList($directory,$level = 0) {

  //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>" . str_repeat('--',$level) . $folder . "</strong>  <a href=\"javascript:show('" . $folder . "');\">Show</a>- <a href=\"javascript:hide('" . $folder . "');\">Hide</a><br />";

    echo "<div id=\"" . $folder . "\">";
    dirList($directory . $folder . '/',$level + 1);

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

  }

  foreach ($files as $file) {
    echo  str_repeat('--',$level) . $file . "<br />";
  }

}

dirList($directory);
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.