PHP遍历目录和文件列表

对于PHP遍历目录和文件列表,下面写了一个简单的类,并附上使用实例,如有所疏漏及错误,请大家指点!
  1. <?php  
  2. define('DS', DIRECTORY_SEPARATOR);  
  3.   
  4. class getDirFile{  
  5.   
  6.     //返回数组  
  7.     private $DirArray  = array();  
  8.     private $FileArray = array();  
  9.     private $DirFileArray = array();  
  10.   
  11.     private $Handle,$Dir,$File;  
  12.   
  13.     //获取目录列表  
  14.     public function getDir( & $Dir ){  
  15.         ifis_dir($Dir) ){  
  16.             if( false != ($Handle = opendir($Dir)) ){  
  17.                 while( false != ($File = readdir($Handle)) ){  
  18.                     if$File!='.' && $File!='..' && !strpos($File,'.') ){  
  19.                         $DirArray[] = $File;  
  20.                     }  
  21.                 }  
  22.                 closedir$Handle );  
  23.             }  
  24.         }else{  
  25.             $DirArray[] = '[Path]:\''.$Dir.'\' is not a dir or not found!';  
  26.         }  
  27.         return $DirArray;  
  28.     }  
  29.   
  30.     //获取文件列表  
  31.     public function getFile( & $Dir ){  
  32.         ifis_dir($Dir) ){  
  33.             if( false != ($Handle = opendir($Dir)) ) {  
  34.                 while( false != ($File = readdir($Handle)) ){  
  35.                     if$File!='.' && $File!='..' && strpos($File,'.') ){  
  36.                         $FileArray[] = $File;  
  37.                     }  
  38.                 }  
  39.                 closedir$Handle );  
  40.             }  
  41.         }else{  
  42.             $FileArray[] = '[Path]:\''.$Dir.'\' is not a dir or not found!';  
  43.         }  
  44.         return $FileArray;  
  45.     }  
  46.   
  47.     //获取目录/文件列表  
  48.     public function getDirFile( & $Dir ){  
  49.         ifis_dir($Dir) ){  
  50.             $DirFileArray['DirList'] = $this->getDir( $Dir );  
  51.             if$DirFileArray ){  
  52.                 foreach$DirFileArray['DirList'as $Handle ){  
  53.                     $File = $Dir.DS.$Handle;  
  54.                     $DirFileArray['FileList'][$Handle] = $this->getFile( $File );  
  55.                 }  
  56.             }  
  57.         }else{  
  58.             $DirFileArray[] = '[Path]:\''.$Dir.'\' is not a dir or not found!';  
  59.         }  
  60.         return $DirFileArray;  
  61.     }  
  62.   
  63. }  
  64. ?>   


实例:(相对路径或绝对路径)

1.获取目录列表

  1. <?php  
  2. $Dir_dir  = './example';  
  3.   
  4. $getDirFile = new getDirFile();  
  5. $getDir = $getDirFile->getDir( $Dir_dir );  
  6.   
  7. print_r($getDir);  
  8. ?>  
显示:
  1. Array  
  2. (  
  3.     [0] => example_one  
  4.     [1] => example_two  
  5. )  

2.获取文件列表

  1. <?php  
  2. $File_one_dir = './example/example_one';  
  3. $File_two_dir = 'E:/Workspace/mycode/getDirFile/example/example_two';  
  4.   
  5. $getDirFile = new getDirFile();  
  6. $getFile_one = $getDirFile->getFile( $File_one_dir );  
  7. $getFile_two = $getDirFile->getFile( $File_two_dir );  
  8.   
  9. print_r($getFile_one);  
  10. print_r($getFile_two);  
  11. ?>  
显示:
  1. Array  
  2. (  
  3.     [0] => example.sql  
  4.     [1] => example.txt  
  5. )  
  6.   
  7. Array  
  8. (  
  9.     [0] => example.php  
  10. )  

3.获取目录/文件列表

  1. <?php  
  2. $Dir_dir  = './example';  
  3.   
  4. $getDirFile = new getDirFile();  
  5. $getDirFile  = $getDirFile->getDirFile( $Dir_dir );  
  6.   
  7. print_r($getDirFile);  
  8. ?>  
显示:
  1. Array  
  2. (  
  3.     [DirList] => Array  
  4.         (  
  5.             [0] => example_one  
  6.             [1] => example_two  
  7.         )  
  8.   
  9.     [FileList] => Array  
  10.         (  
  11.             [example_one] => Array  
  12.                 (  
  13.                     [0] => example.sql  
  14.                     [1] => example.txt  
  15.                 )  
  16.   
  17.             [example_two] => Array  
  18.                 (  
  19.                     [0] => example.php  
  20.                 )  
  21.   
  22.         )  
  23.   

转载于:https://www.cnblogs.com/wepe/p/7424489.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值