PHP readdir function usage example, readdir function example
The example in this article describes the usage of readdir function in php. Share it with everyone for your reference. The specific usage analysis is as follows:
Definition and usage: The readdir() function returns the entry in the directory handle opened by opendir(). If successful, the function returns a file name, otherwise it returns false.
Example 1, the code is as follows:
Copy code The code is as follows:
$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);
}
}
Example 2,
Note that the !== operator did not exist before 4.0.0-RC2, The code is as follows:
Copy code 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 */
While (false !== ($file = readdir($handle))) {
echo "$file ";
}
/* This is the wrong way to traverse the directory */
While ($file = readdir($handle)) {
echo "$file ";
}
closedir($handle);
}
Example 3, readdir() will return . and .. entries. If you don’t want them, just filter them out. Example 2. List all files in the current directory and remove . and .., the code is as follows:
Copy code 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.
I hope this article will be helpful to everyone’s PHP programming design.
http://www.bkjia.com/PHPjc/910592.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/910592.htmlTechArticlePHP’s readdir function usage example, readdir function example This article describes the use of the readdir function in php. Share it with everyone for your reference. The specific usage analysis is as follows: Definition and usage:...