The Readdir () function returns the file name of the next file in the directory. Returns the entry name (file name) if successful, and FALSE if it fails.
Grammar
Readdir (Dir_handle);
Parameters |
Description |
Dir_handle |
Optional. Specifies the directory handle resource that was previously opened by Opendir (). If this parameter is not specified, the last link opened by Opendir () is used. |
Readdir () Function instance one, the code is as follows:
$dir = "readdir/"; Determines whether the directory if (Is_dir ($dir)) { if ($dh = Opendir ($dir)) {while (($file = Readdir ($DH))!== false) { echo ' fil ename: $file: filetype: ". FileType ($dir. $file). " "; } Closedir ($DH); } }
Readdir () Function instance Two, the code is as follows:
if ($handle = Opendir ('/path/to/files ')) { echo "Directory handle: $handle"; echo "Files:"; /* This is the correct way to traverse the directory method * /while (false!== ($file = Readdir ($handle))) { echo "$file"; } /* This is the wrong way to traverse the directory * /while ($file = Readdir ($handle)) { echo "$file"; } Closedir ($handle); }
Readdir () Function instance three, Readdir () will be returned. And.. Entries, if you do not want them, just filter out, example 2. List all files in the current directory and remove them. And.., the code is as follows:
if ($handle = Opendir ('. ')) {while (false!== ($file = Readdir ($handle))) { if ($file! = "." && $file! = "...") { echo "$file"; } } Closedir ($handle); }
Note:Readdir must be used in conjunction with Opendir.