简体   繁体   English

PHP:使用scandir从多个子目录中获取所有zip文件

[英]PHP: Get all zip file from multiple sub-directories using scandir

I have a folder store_files under that I have multiple sub-folders. 我有一个文件夹store_files,该文件夹下有多个子文件夹。 Now I can get all folders and all the files. 现在,我可以获得所有文件夹和所有文件。 Now I want to get all folders and nothing else but zip files. 现在,我要获取所有文件夹,而除zip文件外别无其他。 I try to add condition that if file extension is equal to zip but seems not working. 我尝试添加条件,如果文件扩展名等于zip,但似乎不起作用。 If I add the $ext == 'zip' nothing show in the output. 如果我添加$ ext =='zip',则输出中不显示任何内容。 Is there another way to get specific file like for example in images there's a glob($getDirectory. '*.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');

I think that your problem stems from the fact that scandir() isn't (by itself) a recursive directory search, it only lists the files and directories directly in the specified directory. 我认为您的问题源于scandir()本身不是递归目录搜索的事实,它仅直接在指定目录中列出文件和目录。 You have accounted for that already with your recursive call to your listFolderFiles() function if you detect that $ff is a directory. 如果您检测到$ff是目录,则已经通过递归调用listFolderFiles()函数解决了这一问题。 However, your problem is that you have already filtered out directories in your previous if statement (assuming your directories don't end in '.zip'). 但是,您的问题是您已经在先前的if语句中过滤掉了目录(假设目录未以“ .zip”结尾)。 I think the below function is what you are after: 我认为以下功能是您所追求的:

            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');

I don't think that using glob will be appropriate for this context: 我认为使用glob不适合这种情况:

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

will only get zip files from the sub-directories and not the parent directory . 只会从子目录而不是父目录中获取zip文件。

you may need to use RecursiveDirectoryIterator , this will be more appropriate 您可能需要使用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";
    }
}

thanks goes to this comment submitter . 谢谢这个评论提交者

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM