This article illustrates the use of Readdir functions in PHP. Share to everyone for your reference. The specific usage analysis is as follows:
Definition and Usage: the Readdir () function returns an entry in a table of contents handle opened by Opendir (), and if successful, the function returns a filename, or false.
Instance one, the code is as follows:
Copy Code code as follows:
$dir = "readdir/";
To determine whether 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 two, Note that the!== operator does not exist before 4.0.0-RC2,The code is as follows:
Copy Code code as follows:
if ($handle = Opendir ('/path/to/files ')) {
echo "Directory handle: $handle";
echo "Files:";
/* This is the right 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);
}
Instance three, Readdir () will be returned. And.. Items, if you don't want them, just filter it out, Example 2. Lists all files in the current directory and removes them. And.., the code is as follows:
Copy Code code 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.
I hope this article will help you with your PHP program design.