Connecting Tech Pros Worldwide Help | Site Map

Get List of Files/Folders from a specificed directory?

Familiar Sight
 
Join Date: Sep 2008
Posts: 252
#1: Aug 25 '09
Is it possible to get a lis t of files and folder from a specificed directory using PHP?
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#2: Aug 25 '09

re: Get List of Files/Folders from a specificed directory?


of course, see readdir() comments.
Familiar Sight
 
Join Date: Sep 2008
Posts: 252
#3: Aug 25 '09

re: Get List of Files/Folders from a specificed directory?


I'm trying to make it recurse through a sub-folder if it finds one, can anyone advise, heres the code I'm using?
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $rootDir = 'test_folder/';
  3.  
  4. if ($handle = opendir($rootDir)) {
  5.     echo "<strong>Directory handle:</strong> $handle<br />";
  6.     echo "<strong>Files:</strong><br/>";
  7.  
  8.     while (false !== ($file = readdir($handle))) {
  9.         if(strpos($file,'.') {
  10.             if ($handle2 = opendir($rootDir.$file)) {
  11.                 while (false !== ($file2 = readdir($handle2))) {
  12.                     echo "&nbsp;&nbsp;&nbsp;$file2<br/>";
  13.                 }
  14.             }
  15.         }
  16.         else {
  17.             echo "$file<br/>";
  18.         }
  19.     }
  20.  
  21.     closedir($handle);
  22. }
  23. ?>
Newbie
 
Join Date: Aug 2009
Posts: 1
#4: Aug 25 '09

re: Get List of Files/Folders from a specificed directory?


If you go to the link that Dormilich mentioned, http://us2.php.net/readdir, the latest post has a recursive function that should do what you want. What you have isn't recursive because it isn't a function. It can't call itself.
Familiar Sight
 
Join Date: Sep 2008
Posts: 252
#5: Aug 26 '09

re: Get List of Files/Folders from a specificed directory?


I got it working, it'll list the root directory and any sub-directories in the root directory, also it will remove '.' and '..'.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. getDirectoryListings('test_folder/');
  3.  
  4. function getDirectoryListings($rootDir) {
  5.     if ($handle = opendir($rootDir)) {
  6.         print "<strong>Directory handle:</strong> $handle<br />";
  7.         print "<strong>Files:</strong><br/>";
  8.         print "<ul>";
  9.  
  10.         while (false !== ($file = readdir($handle))) {
  11.             if(($file != '.') && ($file != '..')) {
  12.                 if(!strpos($file,'.')) {
  13.                     print "<li>$file</li>";
  14.                     print "<ul>";
  15.                     if ($handle2 = opendir($rootDir.$file)) {
  16.                         while (false !== ($file2 = readdir($handle2))) {
  17.                             if(($file2 != '.') && ($file2 != '..')) {
  18.                                 echo "<li>$file2</li>";
  19.                             }
  20.                         }
  21.                     }
  22.                     print "</ul>";
  23.                 }
  24.                 else {
  25.                     echo "<li>$file</li>";
  26.                 }
  27.             }
  28.         }
  29.         print "</ul>";
  30.  
  31.         closedir($handle);
  32.     }
  33. }
  34. ?>
Reply