Search code examples
phpregexphp-ziparchive

easiest way to get only folder names with .zip in php


I have seen a few example on the web but they seem pretty messy. I am looking for a nice and clean way to say, get me only the files/folders that have .zip on them. What I have so far is:

    foreach(scandir(__DIR__) as $files) {
        var_dump($files);
    }

What I wonder is is if I need pre match or if the ZipArchive class has any functions that state "return only files with .zip


Solution

  • This should work for you, you can use glob():

    <?php
    
        foreach (glob("*.zip") as $filename) {
            echo $filename . "<br />";
        }
    
    ?>
    

    possible Output:

    test - Kopie.zip
    test.zip
    test2.zip
    

    For more information about glob() see the manual: http://php.net/manual/en/function.glob.php