PHP 遍历文件

一:使用scandir 函数

1.1 函数封装
scandir : 是php 自带函数,返回当前目录下的所有文件和文件夹。注意:会有 ‘.’ 和’..’,分别代编当前目录和上层目录。

/**
 * 使用scandir 遍历目录
 *
 * @param $path
 * @return array
 */
function getDir($path)
{
    //判断目录是否为空
    if(!file_exists($path)) {
        return [];
    }

    $files = scandir($path);
    $fileItem = [];
    foreach($files as $v) {
        $newPath = $path .DIRECTORY_SEPARATOR . $v;
        if(is_dir($newPath) && $v != '.' && $v != '..') {
            $fileItem = array_merge($fileItem, getDir($newPath));
        }else if(is_file($newPath)){
            $fileItem[] = $newPath;
        }
    }

    return $fileItem;
}

1.2程序调用 :

echo('遍历目录开始');
$path = realpath('./m1/');
var_dump(getDir($path));
echo('遍历目录结束');

1.3 显示结果

/usr/local/opt/php@5.6/bin/php
遍历目录开始array(9) {
[0]=>
string(48) “/Users/niedewang/PhpstormProjects/test/m1/1.html”
[1]=>
string(48) “/Users/niedewang/PhpstormProjects/test/m1/2.html”
[2]=>
string(50) “/Users/niedewang/PhpstormProjects/test/m1/demo.txt”
[3]=>
string(54) “/Users/niedewang/PhpstormProjects/test/m1/m2/m2_1.html”
[4]=>
string(54) “/Users/niedewang/PhpstormProjects/test/m1/m2/m2_2.html”
[5]=>
string(54) “/Users/niedewang/PhpstormProjects/test/m1/m2/m2_3.html”
[6]=>
string(57) “/Users/niedewang/PhpstormProjects/test/m1/m2/m3/m3_1.html”
[7]=>
string(56) “/Users/niedewang/PhpstormProjects/test/m1/m2/m3/m3_2.php”
[8]=>
string(56) “/Users/niedewang/PhpstormProjects/test/m1/m2/m3/m3_3.txt”
}
遍历目录结束
Process finished with exit code 0

1.4 注意事项

  • 难度系数:简单
  • 注意事项: 注意循环的时候,过滤 ‘.’ 和 ‘..’ ,否则可能会导致死循环

二:使用glob函数

2.1 函数封装
glob:是php 子系统自带函数,功能和scandir 类似,但比它更加强大灵活。
比如 :glob(‘*.txt’),将会列举出目录下的所有为站名为.txt 的文件。

/**
 * 使用glob 遍历
 * @param $path
 */
function getDir2($path)
{

    //判断目录是否为空
    if(!file_exists($path)) {
        return [];
    }

    $fileItem = [];

    //切换如当前目录
    chdir($path);

    foreach(glob('*') as $v) {
        $newPath = $path . DIRECTORY_SEPARATOR . $v;
        if(is_dir($newPath)) {
            $fileItem = array_merge($fileItem,getDir2($newPath));
        }else if(is_file($newPath)) {

            $fileItem[] = $newPath;
        }
    }

    return $fileItem;
}

2.2 函数调用方式

echo('遍历目录开始');
$path = realpath('./m1/');
var_dump(getDir2($path));
echo('遍历目录结束');

2.3 :显示结果,同1.3 一致,不再展示
2.4 注意事项

    • 难易程度:非常容易,不需要过滤 . 和 ..
    • 灵活程度:非常灵活

三:使用opendir 函数
3.1 函数封装
opendir : 返回一个目录句柄,可以使用readdir 读取这个目录,也可以通过closedir 关闭这个目录

/**
 * 使用opendir 函数递归
 */

function getDir3($path)
{
    if(!file_exists($path)) {
        return [];
    }
    $handle = opendir($path);
    $fileItem = [];
    if($handle) {
        while(($file = readdir($handle)) !== false) {
            $newPath = $path . DIRECTORY_SEPARATOR . $file;
            if(is_dir($newPath) && $file != '.' && $file != '..') {

                $fileItem = array_merge($fileItem,getDir3($newPath));
            }else if(is_file($newPath)) {
                $fileItem[] = $newPath;
            }
        }
    }
    @closedir($handle);

    return$fileItem;
}

3.2 调用方式

echo('遍历目录开始');
$path = realpath('./m1/');
var_dump(getDir5($path));
echo('遍历目录结束');

3.3 注意事项
需要过滤 ‘.’ 和 ”

四:使用dir 函数
4.1 和opendir 函数非常类似,它更加的面向对象。

/**
 * dir 函数的方式,对象
 *
 * @param $path
 * @return array
 */
function getDir4($path) {

    if(!file_exists($path)) {
        return [];
    }
    $handel = dir($path);
    $fileItem = [];
    if($handel) {
        while(($file = $handel->read()) !== false) {
            $newPath = $path . DIRECTORY_SEPARATOR . $file;
            if(is_dir($newPath) && $file != '.' && $file != '..') {

                $fileItem = array_merge($fileItem,getDir4($newPath));
            }else if(is_file($newPath)) {
                $fileItem[] = $newPath;
            }
        }
    }
    $handel->close();

    return $fileItem;
}

4.4 注意事项
需要过滤 ‘.’ 和 ”

五:使用spl 库

使用spl 库中的对象,好处是不需要进行迭代,书写起来非常容易

/**
 * 使用 spl 库
 *
 * @param $path
 */
function getDir5($path)
{
    $dir = new RecursiveDirectoryIterator($path);
    $fileItem = [];
    foreach(new RecursiveIteratorIterator($dir) as $k=>$v) {

        $fileName = $v->getBaseName();
        if($fileName != '.' && $fileName != '..') {
            $fileItem[] = $k;
        }
    }

    return $fileItem;
}

5.2 调用方式:

echo('遍历目录开始');
$path = realpath('./m1/');
var_dump(getDir4($path));
echo('遍历目录结束');

5.3 注意事项:

难以程度:容易。
- 类名有点长,但非常容易使用,毕竟不需要自己写迭代函数了。
- 不需要处理 . 和 .. 两个特殊目录

六:总结

  • 如果目录数量不是很多,建议使用 scandir 方式,应为函数名容易记忆。
  • 如果要求灵活性,比如只显示某一类型的文件等,使用glob 方式,
    本身就有类似搜索的功能。
  • 如果目录下文件较多,建议使用opendir 和dir 的方式。
  • 因为scandir 和glob都是返回满足条件的所有文件,如果有几十万这样,返回素组非常庞大。
    opendir循环可控性要强一些,比如可以返回前100个文件等。这个还需要论证
  • 如果希望代码简介,使用spl 库的方式。代码量少
 
原文地址:https://www.cnblogs.com/myzxh/p/10647737.html