-1

Using the following function, I would like to list all the files of a certain path (directories) but without listing (taking into account) the subdirectories situated under that same pathname.

         function listFolderFiles($dir){
            $ffs = scandir($dir);
               echo '<ol>';
         foreach($ffs as $ff){
            if (!is_dir($dir . '/' . $ff)) {
               if( is_file($ff)){
                  listFolderFiles_1($ff);
         }
          echo '</li>';
         }
     }
      echo '</ol>';
   }
   //
   // Array section  

   //Destination data
   $bb = 1;
   $lines_2 = file('C:/Users/TEMP/PHP/Destination_Directory.txt');
   $table = array($lines_2); 
Abigail
  • 1
  • 4

2 Answers2

1

You can list the files simply using glob() function.

<?php
$dir = "/var/www/";


function listFiles( $dir = '') {
    return $files = glob( $dir . "*.*" ); // Using glob() function. You can also apply filters like *.csv, abc*.txt
}

// Call the function
$files = listFiles( $dir );

// Resulting output of files of the directory
foreach ($files as $file ){
    echo basename($file);
}
Surya
  • 454
  • 6
  • 15
  • Thanks for your input. My function ListFolderFiles works but seems that the problem is elsewhere ie. my array function which goes like please check array section in code above. The array function does not differentiate between a folder and a file. – Abigail Jan 08 '15 at 20:15
0

Use this code to list all files in a directory.

function listFolderFiles($dir){
    $items = scandir($dir);

    foreach($items as $item){
        if(!is_dir($dir .'/' .$item)){
            echo $item;
            echo '<br/>';
        }
    }
}

Note this will not include files under sub-directories.

hfatahi
  • 203
  • 2
  • 8