Peuplarchie Posted April 13, 2009 Share Posted April 13, 2009 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 ! Quote Link to comment https://forums.phpfreaks.com/topic/153795-fix-recursive-function-on-a-directory-listing/ Share on other sites More sharing options...
koolswans Posted April 18, 2009 Share Posted April 18, 2009 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/"); Quote Link to comment https://forums.phpfreaks.com/topic/153795-fix-recursive-function-on-a-directory-listing/#findComment-813030 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.