PHP遍历目录和文件列表
发布时间:2020-05-26 01:08:54 所属栏目:PHP 来源:互联网
导读:PHP遍历目录和文件列表
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 <?php define('DS',DIRECTORY_SEPARATOR); class getDirFile{ //返回数组 private $DirArray = array(); private $FileArray = array(); private $DirFileArray = array(); private $Handle,$Dir,$File; //获取目录列表 public function getDir( & $Dir ){ if( is_dir($Dir) ){ if( false != ($Handle = opendir($Dir)) ){ while( false != ($File = readdir($Handle)) ){ if( $File!='.' && $File!='..' && !strpos($File,'.') ){ $DirArray[] = $File; } } closedir( $Handle ); } }else{ $DirArray[] = '[Path]:''.$Dir.'' is not a dir or not found!'; } return $DirArray; } //获取文件列表 public function getFile( & $Dir ){ if( is_dir($Dir) ){ if( false != ($Handle = opendir($Dir)) ) { while( false != ($File = readdir($Handle)) ){ if( $File!='.' && $File!='..' && strpos($File,'.') ){ $FileArray[] = $File; } } closedir( $Handle ); } }else{ $FileArray[] = '[Path]:''.$Dir.'' is not a dir or not found!'; } return $FileArray; } //获取目录/文件列表 public function getDirFile( & $Dir ){ if( is_dir($Dir) ){ $DirFileArray['DirList'] = $this->getDir( $Dir ); if( $DirFileArray ){ foreach( $DirFileArray['DirList'] as $Handle ){ $File = $Dir.DS.$Handle; $DirFileArray['FileList'][$Handle] = $this->getFile( $File ); } } }else{ $DirFileArray[] = '[Path]:''.$Dir.'' is not a dir or not found!'; } return $DirFileArray; } } ?> 实例:(相对路径或绝对路径) 1.获取目录列表 <?php $Dir_dir = './example'; $getDirFile = new getDirFile(); $getDir = $getDirFile->getDir( $Dir_dir ); print_r($getDir); ?> 显示: [html] view plaincopy Array ( [0] => example_one [1] => example_two ) 2.获取文件列表 <?php $File_one_dir = './example/example_one'; $File_two_dir = 'E:/Workspace/mycode/getDirFile/example/example_two'; $getDirFile = new getDirFile(); $getFile_one = $getDirFile->getFile( $File_one_dir ); $getFile_two = $getDirFile->getFile( $File_two_dir ); print_r($getFile_one); print_r($getFile_two); ?> 显示: Array ( [0] => example.sql [1] => example.txt ) Array ( [0] => example.php ) 3.获取目录/文件列表 <?php $Dir_dir = './example'; $getDirFile = new getDirFile(); $getDirFile = $getDirFile->getDirFile( $Dir_dir ); print_r($getDirFile); ?> 显示: Array ( [DirList] => Array ( [0] => example_one [1] => example_two ) [FileList] => Array ( [example_one] => Array ( [0] => example.sql [1] => example.txt ) [example_two] => Array ( [0] => example.php ) ) ) 以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |