Jump to content

recusive scandir


thibo1025
 Share

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...