0

I have been trying to find the correct PHP function to do the following:

PHP function that reads a directory and collects all the filenames (and subdirectory names) into an array.

I have been searching for a while, not to sure on what exactly is the correct answer.

Any help would be appreciated.

Thanks.

  • check [php directory iterator](http://php.net/manual/en/class.directoryiterator.php) – mamdouh alramadan May 04 '14 at 08:48
  • @mamdouhalramadan Here is even better: http://stackoverflow.com/questions/2014474/php-read-sub-directories-and-loop-through-files-how-to – Linek May 04 '14 at 08:55
  • I had similar question, but it depends whicj result (array structure) do you need http://stackoverflow.com/questions/19141998/simple-array-into-associative-array-based-on-directory-tree-like-values-of-th – el Dude Jun 14 '14 at 10:03

2 Answers2

0

See http://www.php.net/manual/en/function.opendir.php - to open directory

Links to read directory.

Would imagine there is an example

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Ed Heal
  • 59,252
  • 17
  • 87
  • 127
-1

see this question

<?php 
function dirToArray($dir) { 

   $result = array(); 

   $cdir = scandir($dir); 
   foreach ($cdir as $key => $value) 
   { 
      if (!in_array($value,array(".",".."))) 
      { 
         if (is_dir($dir . DIRECTORY_SEPARATOR . $value)) 
         { 
            $result[$value] = dirToArray($dir . DIRECTORY_SEPARATOR . $value); 
         } 
         else 
         { 
            $result[] = $value; 
         } 
      } 
   } 

   return $result; 
} 
?>
Community
  • 1
  • 1
Ghilas BELHADJ
  • 13,412
  • 10
  • 59
  • 99
  • I just need the name to the function, not the actual code implementing it. –  May 04 '14 at 09:13