闲着没事,发个PHP遍历目录的代码,供大家参考。
<?php
class listDir {
    var $dirPath;
    function openDir($dirPath) {
        $this->dirPath = $dirPath;
        if (is_dir($dirPath)) {
            $dir = opendir($dirPath);
            return $dir;
        }else {
            die("$dirPath is Not a Directory");
        }
    }
    function closeDir($dir) {
        closedir($dir);
    }
    function listDir($dir) {
        echo '<ol>';
        while($file = readdir($dir)) {
            if($file!='.' && $file!='..') { // filter . and ..
                $dd  = $this->dirPath;  //
                $dd  = $dd.'/'.$file;
                echo "<li>$file</li>";
            }
            if(is_dir($dd) && $file!='.' && $file!='..') { // is_dir 参数需要完整的路径
                $subDir = $this->openDir($dd);
                $this->listDir($subDir);
                $this->closeDir($subDir);
            }
        }
        echo '</ol>';
        return true;
    }
}$dirOpt = new listDir();
$dirOpt->dirPath = 'C:\AppServ\www\sbp_files';
$dir = $dirOpt->openDir($dirOpt->dirPath);
$dirOpt->listDir($dir);
$dirOpt->closeDir($dir);下面再拷贝一份。
<?php
class listDir {
    var $dirPath;
    function openDir($dirPath) {
        $this->dirPath = $dirPath;
        if (is_dir($dirPath)) {
            $dir = opendir($dirPath);
            return $dir;
        }else {
            die("$dirPath is Not a Directory");
        }
    }
    function closeDir($dir) {
        closedir($dir);
    }
    function listDir($dir) {
        echo '<ol>';
        while($file = readdir($dir)) {
            if($file!='.' && $file!='..') { // filter . and ..
                $dd  = $this->dirPath;  //
                $dd  = $dd.'/'.$file;
                echo "<li>$file</li>";
            }
            if(is_dir($dd) && $file!='.' && $file!='..') { // is_dir 参数需要完整的路径
                $subDir = $this->openDir($dd);
                $this->listDir($subDir);
                $this->closeDir($subDir);
            }
        }
        echo '</ol>';
        return true;
    }
}$dirOpt = new listDir();
$dirOpt->dirPath = 'C:\AppServ\www\sbp_files';
$dir = $dirOpt->openDir($dirOpt->dirPath);
$dirOpt->listDir($dir);
$dirOpt->closeDir($dir);

解决方案 »

  1.   

    看手册<?php 
    $dataDir  = dirname(__FILE__).'/world_vest_base/'; 
         
    try 

          $dir  = new DirectoryIterator($dataDir);       foreach ($dir as $file) 
          { 
            $fileName = $file->getFilename(); 
          } 

    catch (Exception $ex) 
    { } ?> queryphp 简单 MVC ORM框架
    ---------------------------------------------
    http://topic.csdn.net/u/20100310/11/62a60067-b3da-4dee-9d6a-f1baeb2f3f33.html
      

  2.   

    有两个警告 改了下<?php
    class listDirClass {
        var $dirPath;
        function openDir($dirPath) {
            $this->dirPath = $dirPath;
            if (is_dir($dirPath)) {
                $dir = opendir($dirPath);
                return $dir;
            }else {
                die("$dirPath is Not a Directory");
            }
        }
        function closeDir($dir) {
            closedir($dir);
        }
        function listDir($dir) {
            echo '<ol>';
            while($file = @readdir($dir)) {
                if($file!='.' && $file!='..') { // filter . and ..
                    $dd  = $this->dirPath;  //
                    $dd  = $dd.'/'.$file;
                    echo "<li>$file</li>";
                }
                if(is_dir($dd) && $file!='.' && $file!='..') { // is_dir 参数需要完整的路径
                    $subDir = $this->openDir($dd);
                    $this->listDir($subDir);
                    $this->closeDir($subDir);
                }
            }
            echo '</ol>';
            return true;
        }
    }$dirOpt = new listDirClass();
    $dirOpt->dirPath = 'D:\minzheng\DBUnity\obj';
    $dir = $dirOpt->openDir($dirOpt->dirPath);
    $dirOpt->listDir($dir);
    $dirOpt->closeDir($dir);?> 
      

  3.   

    DirectoryIterator 很好用 lz的古董方法收回吧 估计是看的4.几的书
      

  4.   

    对于PHP这种解释性的语言,能一个函数就搞定的就不要用class
      

  5.   


    我也这么觉得,所以我不会写class ...为什么那么多人都喜欢写class ?不太习惯
      

  6.   

    directoryitertor 这个不错,多谢楼上的分享