php readdir函数用法与readdir实例
定义和用法
readdir() 函数返回由 opendir() 打开的目录句柄中的条目,若成功,则该函数返回一个文件名,否则返回 false。
实例一
$dir = "readdir/"; // 判断是否为目录 if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { echo "filename: $file : filetype: " . filetype($dir . $file) . " "; } closedir($dh); } }
//实例二
// 注意在 4.0.0-RC2 之前不存在 !== 运算符
if ($handle = opendir('/path/to/files')) { echo "Directory handle: $handle "; echo "Files: "; /* 这是正确地遍历目录方法 */ while (false !== ($file = readdir($handle))) { echo "$file "; } /* 这是错误地遍历目录的方法 */ while ($file = readdir($handle)) { echo "$file "; } closedir($handle); }
//实例三 readdir() 将会返回 . 和 .. 条目。如果不想要它们,只要过滤掉即可: 例子 2. 列出当前目录的所有文件并去掉 . 和 ..
if ($handle = opendir('.')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { echo "$file "; } } closedir($handle); }
//注:readdir必须与opendir配合使用才行。
永久地址:http://www.phprm.com/function/0da7b210f7f97e3b3ba1e377e96aeae6.html
转载随意~请带上教程地址吧^^