zoukankan      html  css  js  c++  java
  • php遍历文件的5种方式

    1.使用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.2 调用方式

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

       4.3 注意事项 
             需要过滤 "." 和 ".."

  • 相关阅读:
    在Ubuntu下安装TensorFlow-gpu版本
    ubuntu安装和卸载软件命令
    Window7通过Anaconda安装Tensorflow
    Kaggle Titanic Data Science Solutions
    CNN、RNN和DNN的区别
    卷积神经网络介绍
    大数据随想
    golang omitempty 总结
    LeetCode 856 递归思路详解
    安装Nginx的各种报错的解决
  • 原文地址:https://www.cnblogs.com/mzhaox/p/11218266.html
Copyright © 2011-2022 走看看