readdir

(PHP 3, PHP 4 >= 4.0.0)

readdir -- read entry from directory handle

Description

string readdir (resource 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


// Note that !== did not exist until 4.0.0-RC2
<?php
$handle=opendir('.');
echo "Directory handle: $handle\n";
echo "Files:\n";
while (false !== ($file = readdir($handle))) { 
    echo "$file\n";
}
closedir($handle); 
?>
      

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 ..


<?php 
$handle = opendir('.'); 
while (false !== ($file = readdir($handle))) { 
    if ($file != "." && $file != "..") { 
        echo "$file\n"; 
    } 
}
closedir($handle); 
?>