首页 > 编程 > PHP > 正文

php遍历文件夹和文件列表示例分享

2020-03-22 17:33:04
字体:
来源:转载
供稿:网友
为PHP遍历目录和文件列表写了一个简单的类,并附上使用实例,大家参考使用吧复制代码 代码如下:
?php
define('DS', DIRECTORY_SEPARATOR);html' target='_blank'>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
)
)
)
PHP教程

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表