一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

php scandir遍历显示所有文件与文件夹下的文件

时间:2011-06-06 编辑:简简单单 来源:一聚教程网

php教程 scandir遍历显示所有文件与文件夹下的文件,方法很简单我们只要利用is_dir判断再递归查找一次,这样就可以把遍历目录下所有文件了。

目录遍历

  function numfilesindir ($thedir){
    if (is_dir ($thedir)){
      $scanarray = scandir ($thedir);
      for ($i = 0; $i < count ($scanarray); $i++){
        if ($scanarray[$i] != "." && $scanarray[$i] != ".."){
          if (is_file ($thedir . "/" . $scanarray[$i])){
            echo $scanarray[$i] . "
";
          }
        }
      }
    } else {
      echo "Sorry, this directory does not exist.";
    }
  }
  echo numfilesindir ("sample1");

?>

扫描指定位置的文件

   print_r(scandir("/usr/local/apache2/htdocs"));
?>

    $files = scandir(".", 1);
    var_dump($files);
?>

热门栏目