php获取目录下的所有文件和子目录

时间:2016-08-06
php可以使用readdir()函数获取目录下的文件和子目录,本文章向大家介绍如何使用readdir()函数获取目录下的文件和子目录,需要的朋友可以参考一下。

php获取目录下的所有文件和子目录,主要使用的文件操作函数readdir(),具体实现代码如下:

<?php
$dirname = ".";
$dh = opendir( $dirname );
while ( ! is_bool( $file = readdir( $dh )) ) {
  if ($file!= "." && $file!= ".."){
    if ( is_dir( "$dirname/$file" ) ) {
      print "(D) ";
    }
    print "$file<br/>";
  }
}
/* http://www.manongjc.com/article/1317.html */
closedir( $dh );
?>

小编本地运行结果如下:

active.php
(D) codepress
email.php
index.php
(D) java
(D) myapp
(D) mypan
sales.csv
sat_score.php
saveme.txt
test.php
testmysql.php
(D) th