PHP readdir Function Usage example, Readdir function instance
This example describes 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 directory handle opened by Opendir (), and if successful, returns a file name, otherwise false.
Example one, the code is as follows:
Copy CodeThe code is as follows: $dir = "readdir/";
Determine if the directory
if (Is_dir ($dir)) {
if ($dh = Opendir ($dir)) {
while (($file = Readdir ($DH))!== false) {
echo "FileName: $file: filetype:". FileType ($dir. $file). " ";
}
Closedir ($DH);
}
}
Example Two,Note that the!== operator does not exist before 4.0.0-rc2.The code is as follows:
Copy CodeThe 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);
}
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:
Copy CodeThe 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.
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/910592.html www.bkjia.com true http://www.bkjia.com/PHPjc/910592.html techarticle PHP's Readdir function Usage example, Readdir Function Example This article describes the Readdir function usage in PHP. Share to everyone for your reference. The specific usage analysis is as follows: definition and usage: ...