Jump to content

recusive scandir


thibo1025

Recommended Posts

Hello W3C's,I would like to create a Php function to show teh content of a dir like this.

  • dir 1
    • dir 1.1
      • file
      • 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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...