thibo1025 Posted April 7, 2010 Share Posted April 7, 2010 Hello W3C's,I would like to create a Php function to show teh content of a dir like this. dir 1dir 1.1file file [*]file [*]file [*]dir 2 file file [*]file[*]file And I cannot figure out how to scan dir1.1 properly.any help Link to comment Share on other sites More sharing options...
justsomeguy Posted April 7, 2010 Share Posted April 7, 2010 This is the general theory of a recursive function: function list_directory($dir){ $dirs = array(); $files = array(); $contents = scandir($dir); foreach ($contents as $item) { // if $item is a directory, add it to the $dirs array, // else add it to the $files array } // sort $dirs and $files arrays foreach ($dirs as $d) { // print directory name list_directory($dir . PATH_SEPARATOR . $d); // recurse } foreach ($files as $f) { // print file list }} All the function does is get the contents of the directory, build a list of directories and files, run itself for each directory, then print the list of files. That will produce output in the order you showed. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.