$dir = "readdir/"; // 判断是否为目录 if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { echo "filename: $file : filetype: " . filetype($dir . $file) . " "; } closedir($dh); } }
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); }
if ($handle = opendir('.')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { echo "$file "; } } closedir($handle); }
希望本文所述对大家的php程序设计有所帮助。