首页 > php代码 > php scandir遍历显示所有文件与文件夹下的文件

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

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

目录遍历

<?php
  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] . "<br />";
          }
        }
      }
    } else {
      echo "Sorry, this directory does not exist.";
    }
  }
  echo numfilesindir ("sample1");

扫描指定位置的文件

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

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

文章地址:http://www.phprm.com/code/38877.html

转载随意^^请带上本文地址!

标签:遍历目录

相关文章

发表留言