当前位置: 移动技术网 > IT编程>开发语言>PHP > php遍历文件夹和文件列表示例分享

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

2019年03月28日  | 移动技术网IT编程  | 我要评论

为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
                )
        )
)

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网