1

I am gathering list of files in given folder, where there might be files and folders.

I can manually skip folder when I want, but how can I skip ALL folders automatically and store only files in the array?

$moduleArray = array_diff(scandir('module/', 1), array('..', '.', 'admin'));

Well, I got this... But any way to do it while searching using scandir?

    foreach($moduleArray as $module) {
        $end = explode(".", $module);
        if(end($end) == "php") {
            $name = substr($module, 0, -8);
            echo " <a href=\"index.php?page=$name\"> $name </a><br />";
        }
    }
HelpNeeder
  • 6,383
  • 24
  • 91
  • 155

3 Answers3

2

Alternatively, you could use SPL DirectoryIterator's ->isDir() too:

$moduleArray = array();
foreach (new DirectoryIterator('module/') as $file) {
    if($file->isDir()) continue;
    $moduleArray[] = $file->getPathname(); // or getFilename()
}
Kevin
  • 41,694
  • 12
  • 53
  • 70
  • +1 Waiting for somebody to throw this solution up :) (*maybe note the php version required for this class*) – Darren Jul 24 '14 at 02:56
  • hello @Darren yeah i use this one too, it doesn't specifically say which PHP version this library is bundled with (5.3 and above?)[manual](http://php.net/manual/en/class.directoryiterator.php) it only says here `5` – Kevin Jul 24 '14 at 03:01
1

You Can Search Array And Remove Folders; This Is The Best Way That I Know.:-)

 for($i=0;$i<count($moduleArray);$i++)
 {
   if(is_dir($moduleArray[$i])
   {
     //Skip It
   }
 }
Hadi Nahavandi
  • 666
  • 7
  • 18
1
$moduleArray = array_diff(scandir('module/', 1), array('..', '.'));
$fileListArray = array();
foreach ($moduleArray as $module){
   if (!is_dir($module)){
      $fileListArray[] = $module;  
   }
}
J A
  • 1,776
  • 1
  • 12
  • 13