Php list ONLY files inside zip archive (exclude folders)

$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 the files inside the zip archive, but I want to exclude the list of folders. 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 show the folder name in the list.

Is there a way to determine if an element is a directory in my foreach loop (how?) Or do I need to run a search in the array and search for folders and then cancel it (how?)?

thanks for the help

+6
source share
2 answers

Your foreach is useless. It iterates over an array one element at a time.

In any case, there are two ways to detect a folder. First, folders end with "/". The second folders are 0 in 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); 
+2
source

Just check the file size if it zeros out its folder.

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

Source: https://habr.com/ru/post/948284/


All Articles