Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search Directories and Sub Directories

Status
Not open for further replies.

likelylad

IS-IT--Management
Jul 4, 2002
388
GB
I need to list all the files in a folder including its sub directories.

I have the following code from skiflyer in thread434-682722 which I believe should work.
Code:
function getAllFiles($path, &$files, $recursive=true) {
  echo "New directory starting: ".$path."\n";
  $dir_handle=opendir($path);
  while ($entry = readdir($dir_handle)) {
    if (is_file($path."/".$entry)) {
      $files[]=$path."/".$entry;
    } else {
      if ($entry != "." && $entry != ".." && $recursive) getAllFiles($path."/".$entry, $files);
    }
  }
}

Say for example I had a folder called c:/test.
I can't figure out the variable to output onto a page.


 
If you are interested theres an excellent piece of code for this at by Gabriel Dunne, and an icon-modded version by yours truly.

Look for DirectoryLister towards the bottom of the page.

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
This looks great but where do I change the directory that it is looking in e.g. c:\test

 
whatever directory its in is the base directory, you can include the file from a different directory if you like.

for example if you are in c:\test\subdir, and you have index.php which include('..\Directorylisterxxx.pp') the root directory shown from the subdir\index.php would be subdir, and no facility to go up a directory wouuld exist.

if you want to hold directorylisterxxx.php in say c:\scripts, place an index.php in c:\test include('c:\\scripts\Directorylisterxxx.php') and you should only see test (theres a section somewhere in it to ensure you cant go higher than root, but most dev was done under apache on linux.

Have a play, the file itself is pretty well documented. (or was last time I looked, really must bring the icon-mod up to date.

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
I just thought I'd let people know what I done.

I have altered skiflyer's code in thread434-682722 slightly to get what I want.

I just wanted to see the files in the folders and subfolders rather than the actual path

Code:
function getFiles($directory) {
   // Try to open the directory
   if($dir = opendir($directory)) {
       // Create an array for all files found
       $tmp = Array();

       // Add the files
       while($file = readdir($dir)) {
           // Make sure the file exists
           if($file != "." && $file != ".." && $file[0] != '.') {
               // If it's a directiry, list all files within it
               if(is_dir($directory . "/" . $file)) {
                   $tmp2 = getFiles($directory . "/" .$file);
                   if(is_array($tmp2)) {
                       $tmp = array_merge($tmp, $tmp2);
                   }
               } else {
                   array_push($tmp, $file);
               }
           }
       }

       // Finish off the function
       closedir($dir);
       return $tmp;
   }
}
$sum= getFiles('c:\test');

print_r(array_count_values($sum));
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top