简体   繁体   中英

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

I have a folder store_files under that I have multiple sub-folders. Now I can get all folders and all the files. Now I want to get all folders and nothing else but zip files. I try to add condition that if file extension is equal to zip but seems not working. If I add the $ext == 'zip' nothing show in the output. Is there another way to get specific file like for example in images there's a 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. You have accounted for that already with your recursive call to your listFolderFiles() function if you detect that $ff is a directory. However, your problem is that you have already filtered out directories in your previous if statement (assuming your directories don't end in '.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:

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

will only get zip files from the sub-directories and not the parent directory .

you may need to use RecursiveDirectoryIterator , this will be more appropriate

$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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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