为PHP遍历目录和文件列表写了一个简单的类,并附上使用实例,大家参考使用吧
复制代码 代码如下:
<?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);
?>
显示
复制代码 代码如下:
<?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);
?>
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
)
)
)
遍历文件夹
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
更新日志
- 华少翌.2005-华少翌【中唱艺能】【WAV+CUE】
- 群星《十二女声Ⅱ》[WAV/CUE/分轨][527MB]
- 大自然音乐系列《漫步在山裡》[WAV/CUE/分轨][502.8MB]
- 大自然音乐系列《星光夜语》[WAV/CUE/分轨][453.3MB]
- 史依弘《史依弘交响乐京剧演唱专辑HQ》头版限量编号[WAV+CUE]
- 曼丽2020《女人三十风继续吹》HQCD[WAV+CUE]
- 曼丽2021《女人三十2雾之恋》HQCD[WAV+CUE]
- 隔壁老樊.2019-我曾【好听音乐】【FLAC分轨】
- 高林生.1993-牵挂你的人是我【白天鹅】【WAV+CUE】
- 洪卓立.2012-找个懂我的女孩(国专)【英皇娱乐】【WAV+CUE】
- 曼丽2022《女人三十3晚秋》HQCD[WAV+CUE]
- 曼丽2018《女人三十4冬天》HQCD[WAV+CUE]
- 柏菲-《好歌30年壹》限量开盘母带ORMCD[低速原抓WAV+CUE]
- 大自然音乐系列《水舞》[WAV/CUE/分轨][424.3MB]
- 大自然音乐系列《星光夜语》[WAV/CUE/分轨][453.3MB]