Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php list ONLY files inside zip archive (exclude folders)

Tags:

arrays

php

        $za = new ZipArchive();
        $za->open($source);
        for( $i = 0; $i < $za->numFiles; $i++ ){
            $stat = $za->statIndex( $i );
            $items = array( basename( $stat['name'] ) . PHP_EOL );
            foreach($items as $item) {
            echo $item;
            }
        }

This code will list all files inside a zip archive but i want to exclude folders listing. If the item in the array is a folder, i want to exclude it from the array BUT i still want to list the files inside the folder. Just don't display the folder's name in the list.

Is there a way i can detect if the item is a directory in my foreach loop (how?) or do i need to run a search on the array and look for folders then unset it (how?) ?

Thanks for your help

like image 473
Petr Kropotkin Avatar asked Jun 28 '13 00:06

Petr Kropotkin


1 Answers

Your foreach is useless. It iteratate over array with one item.

Anyway there is two way to detect folder. First, folders are ended with '/'. Second folders has 0 size.

$za = new ZipArchive();
$za->open('zip.zip');
$result_stats = array();
for ($i = 0; $i < $za->numFiles; $i++)
    {
    $stat = $za->statIndex($i);
    if ($stat['size'])
        $result_stats[] = $stat;
    }

echo count($result_stats);
like image 186
sectus Avatar answered Oct 14 '22 13:10

sectus