【问题标题】:PHP: Get all zip file from multiple sub-directories using scandirPHP:使用 scandir 从多个子目录获取所有 zip 文件
【发布时间】:2017-07-23 18:59:42
【问题描述】:

我有一个文件夹 store_files,下面有多个子文件夹。现在我可以获得所有文件夹和所有文件。现在我想获取所有文件夹,除了 zip 文件之外别无其他。我尝试添加条件,如果文件扩展名等于 zip 但似乎不起作用。如果我添加 $ext == 'zip' ,输出中不会显示任何内容。是否有另一种获取特定文件的方法,例如在图像中有glob($getDirectory. '*.zip')

                function listFolderFiles($dir){
                    $ffs = scandir($dir);
                    echo '<ol>';
                    foreach($ffs as $ff){
                        $ext = pathinfo($ff, PATHINFO_EXTENSION);

                        if($ff != '.' && $ff != '..' && $ext == 'zip'){
                            echo '<li>';
                            $str = preg_replace('/\D/', '', $ff);
                            $str = substr($str, 0,-1);

                            $getYear = substr($str, 0,4);
                            $getMonth = substr($str, -4,2);
                            $getDay = substr($str, -2,2);
                            $theDate = $getYear.'-'.$getMonth.'-'.$getDay;
                            $theRealDate = date('Y M j', strtotime($theDate));
                            echo $theRealDate;


                            if(is_dir($dir.'/'.$ff)){
                             listFolderFiles($dir.'/'.$ff);
                            }

                            echo '</li>';
                        }

                    }
                    echo '</ol>';
                }
                listFolderFiles('store_files');

【问题讨论】:

  • 你为什么不用glob()
  • 不是直接的,glob() 不能递归工作。但是您可以非常轻松地围绕它构建一个包装器。看看那个答案,例如:stackoverflow.com/questions/17160696/…
  • @arkascha 或简单地使用 spl 迭代器
  • 当然glob() 本身不能递归工作。但是可以轻松地将递归包装器构建为围绕它的 2 衬垫。

标签: php arrays


【解决方案1】:

我认为您的问题源于 scandir() 不是(本身)递归目录搜索,它只直接列出指定目录中的文件和目录。如果您检测到 $ff 是一个目录,您已经通过递归调用您的 listFolderFiles() 函数来考虑这一点。但是,您的问题是您已经在之前的 if 语句中过滤掉了目录(假设您的目录不以“.zip”结尾)。我认为以下功能是您所追求的:

            function listFolderFiles($dir){
                $ffs = scandir($dir);
                echo '<ol>';
                foreach($ffs as $ff){
                    $ext = pathinfo($ff, PATHINFO_EXTENSION);

                    if($ff != '.' && $ff != '..' && (is_dir($dir.'/'.$ff) || $ext == 'zip')){
                        echo '<li>';
                        $str = preg_replace('/\D/', '', $ff);
                        $str = substr($str, 0,-1);

                        $getYear = substr($str, 0,4);
                        $getMonth = substr($str, -4,2);
                        $getDay = substr($str, -2,2);
                        $theDate = $getYear.'-'.$getMonth.'-'.$getDay;
                        $theRealDate = date('Y M j', strtotime($theDate));
                        echo $theRealDate;


                        if(is_dir($dir.'/'.$ff)){
                         listFolderFiles($dir.'/'.$ff);
                        }

                        echo '</li>';
                    }

                }
                echo '</ol>';
            }
            listFolderFiles('store_files');

【讨论】:

  • 非常感谢我确实做到了,但这更干净
【解决方案2】:

我认为使用glob 不适合这种情况:

$ffs = glob($dir . "/*/*.zip");

只会从子目录而不是父目录获取 zip 文件。

你可能需要使用RecursiveDirectoryIterator,这样会更合适

$files = new RecursiveDirectoryIterator("./");
$iterator = new RecursiveIteratorIterator($files, RecursiveIteratorIterator::SELF_FIRST);

foreach ($iterator as $file) {
    if (false === strpos($file, '/.') && strpos($file, '.zip')) {
        echo $file, "\n";
    }
}

感谢this comment submitter

【讨论】:

    猜你喜欢
    • 2016-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-22
    • 1970-01-01
    • 2021-03-12
    相关资源
    最近更新 更多