PHP遍历文件目录笔记

      找到之前的一篇笔记,关于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 '
    
    
  1. ';
  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);

一位牛人说,优秀的代码不需要注释!言外之意是说不用注释,阅读者就能很好的理解!笔者没有达到这个水平,还是稍稍注释,其实用到的都是最基本的目录函数。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值