0

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');
BGTabulation BGTabulate
  • 1,677
  • 3
  • 16
  • 39
  • 1
    Why don't you use `glob()`? – arkascha Mar 03 '17 at 15:36
  • Not directly, `glob()` does not work recursively. But you can very easily build a wrapper around it. Take a look at that answer, for example: http://stackoverflow.com/questions/17160696/php-glob-scan-in-subfolders-for-a-file – arkascha Mar 03 '17 at 15:49
  • @arkascha or simply by using spl iterators – hassan Mar 03 '17 at 16:00
  • Certainly `glob()` does not work recursively by itself. But one can easily build recursive wrapper as a 2 liner around it. – arkascha Mar 03 '17 at 16:02

2 Answers2

2

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');
jimstump
  • 36
  • 3
1

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.

hassan
  • 7,812
  • 2
  • 25
  • 36