您的位置:首页 > 编程语言 > PHP开发

PHP遍历文件目录笔记

2009-12-30 15:36 190 查看
      找到之前的一篇笔记,关于PHP遍历目录类。发布出来,加深印象
1: /**


2:  * Open Read Write Close Dir and File


3:  *


4:  * @author        Yaron [xyaron@gmail.com,http://yaron.org.cn]


5:  * @version        0.1


6:  * @package


7:  */


8:


9: class fileDirOpt {


10:     var $dirPath;


11:     function openDir($dirPath){


12:         $this->dirPath    = $dirPath;


13:         if (is_dir($dirPath)){


14:             $dir    = opendir($dirPath);


15:             return $dir;


16:         }else{


17:             die("$dirPath is Not a Directory");


18:         }


19:     }


20:     function closeDir($dir) {


21:         closedir($dir);


22:     }


23:     function listDir($dir){


24:         echo '';


25:         while($file = readdir($dir)){


26:             if($file!='.' && $file!='..'){    // filter . and ..


27:                 $dd        = $this->dirPath;        //


28:                 $dd        = $dd.'/'.$file;


29:                 echo "$file
";


30:             }


31:             if(is_dir($dd) && $file!='.' && $file!='..') {    // is_dir 参数需要完整的路径


32:                 $subDir    = $this->openDir($dd);


33:                 $this->listDir($subDir);


34:                 $this->closeDir($subDir);


35:             }


36:         }


37:         echo '';


38:         return true;


39:     }


40: }


41:


42: $dirOpt    = new fileDirOpt();


43: $dirOpt->dirPath    = 'd:/xampp';


44: $dir    = $dirOpt->openDir($dirOpt->dirPath);


45: $dirOpt->listDir($dir);


46: $dirOpt->closeDir($dir);


一位牛人说,优秀的代码不需要注释!言外之意是说不用注释,阅读者就能很好的理解!笔者没有达到这个水平,还是稍稍注释,其实用到的都是最基本的目录函数。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: