readdir

readdir -- read entry from directory handle

Description

string readdir(int dir_handle);

Returns the filename of the next file from the directory. The filenames are not returned in any particular order.

Example 1. List all files in the current directory

  1 
  2 <?php
  3 $handle=opendir('.');
  4 echo "Directory handle: $handle\n";
  5 echo "Files:\n";
  6 while ($file = readdir($handle)) {
  7     echo "$file\n";
  8 }
  9 closedir($handle); 
 10 ?>
 11       

Note that readdir() will return the . and .. entries. If you don't want these, simply strip them out:

Example 2. List all files in the current directory and strip out . and ..

  1 
  2 <?php 
  3 $handle=opendir('.'); 
  4 while ($file = readdir($handle)) { 
  5     if ($file != "." && $file != "..") { 
  6         echo "$file\n"; 
  7     } 
  8 }
  9 closedir($handle); 
 10 ?>
 11