zoukankan      html  css  js  c++  java
  • PHP递归和非递归遍历文件夹下文件

    
    function readDirFiles($dir){
        $files= [];
        $queue=[realpath($dir)];
    
        $currentPath = current($queue);
        while($currentPath) {
            $path = $currentPath;
            if (is_dir($path) && $handle = opendir($path)) {
                while ($file = readdir($handle)) {
                    if ($file == '.' || $file == '..') continue;
    
                    $filepath = $path . '/' . $file;
    
                    if (is_dir($filepath)) {
                        $queue[] = $filepath;
                    }else {
                        $files[] = $filepath;
                    }
                }
    
                closedir($handle);
            }
    
            $currentPath = next($queue);
        }
    
        return $files;
    }
    
    print_r($readDirFiles('./'));exit;
    
    function readDirFiles2($path, &$files = []){
        if (is_dir($path) && $handle = opendir($path)) {
            while ($file = readdir($handle)) {
    //            if(strpos($file, '.') === 0) {
    //                continue;
    //            }
                if ($file == '.' || $file == '..') continue;
    
                $filePath = $path . '/' . $file;
    
                if (is_dir($filePath)) {
                    readDirFiles2($filePath, $files);
                }else {
                    $files[] = $filePath;
                }
            }
    
            closedir($handle);
        }
    
        return $files;
    }
    
    readDirFiles2('./', $paths);
    print_r($paths);exit;
    
    
  • 相关阅读:
    CSU 1122
    CSU 1256
    CSU 1240
    HDU 1874
    CSU 1004
    Problem F CodeForces 16E
    Problem E CodeForces 237C
    Problem C FZU 1901
    12-30
    2016-12-29
  • 原文地址:https://www.cnblogs.com/qixidi/p/10206076.html
Copyright © 2011-2022 走看看