Jump to content

Fix recursive function on a directory listing


Peuplarchie

Recommended Posts

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

$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 !

Link to comment
Share on other sites

 

Please try below code:

 

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( ' ', ( $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

}

/* How to Call In Windows */

/* Print DIrectory/File Structure of (windows/web) folder*/

//getDirectory("c:\\windows\\web\\");

 

/* How to Call In Linux */

/* Print DIrectory/File Structure of (windows/web) folder*/

getDirectory("/windows/web/");

Link to comment
Share on other sites

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.