Definition and usage
The readdir () function returns the entries in the directory handle opened by opendir (). If yes, the function returns a file name; otherwise, false.
Instance 1
*/
$ Dir = "readdir /";
// Determine whether it is a directory
If (is_dir ($ dir )){
If ($ dh = opendir ($ dir )){
While ($ file = readdir ($ dh ))! = False ){
Echo "filename: $ file: filetype:". filetype ($ dir. $ file )."";
}
Closedir ($ dh );
}
}
// Instance 2
// Note that it does not exist before 4.0.0-RC2! = Operator
If ($ handle = opendir ('/path/to/Files ')){
Echo "Directory handle: $ handle ";
Echo "Files :";
/* This is the correct way to traverse directories */
While (false! ==( $ File = readdir ($ handle ))){
Echo "$ file ";
}
/* This is the method for mistakenly traversing directories */
While ($ file = readdir ($ handle )){
Echo "$ file ";
}
Closedir ($ handle );
}
// Instance 3 readdir () will return the... and .. entries. If you do not want them, just filter them out. Example 2. List all the files in the current directory and remove ..
If ($ handle = opendir ('.')){
While (false! ==( $ File = readdir ($ handle ))){
If ($ file! = "." & $ File! = ".."){
Echo "$ file ";
}
}
Closedir ($ handle );
}
// Note: readdir must be used with opendir.