mudraya
mudraya

Reputation: 99

how to make an array of files which are part of a .zip file

i am trying to make an array of files which are part of a .zip file. In the .zip file are 2 files: image1.jpg and image2.jpg

$zip = new ZipArchive;
        if ($zip->open($_POST['extractfile']) === TRUE) {

            $unzipped = 0; 
            $fails = 0; 
            $total = 0; 
            for ($i = 0; $i < $zip->numFiles; $i++) {
                $path_info = pathinfo($zip->getNameIndex($i));
                $ext = $path_info['extension'];
                $total ++; 

                echo $zip->getNameIndex($i);

The echo outputs only the first file: image1.jpg

How can i make an array of the files which are in the .zip file so that i can use a foreach loop like below:

foreach($extractfiles as $extractfile) {
     echo $extractfile;
}

Upvotes: 1

Views: 160

Answers (1)

Jimmix
Jimmix

Reputation: 6506

To the second part

<?php

$zip = new ZipArchive;

        $extractfiles = [];

        if ($zip->open($_POST['extractfile']) === TRUE) {


            $unzipped = 0; 
            $fails = 0; 
            $total = 0; 
            for ($i = 0; $i < $zip->numFiles; $i++) {
                $path_info = pathinfo($zip->getNameIndex($i));
                $ext = $path_info['extension'];
                $total ++; 

                echo $zip->getNameIndex($i);

                $extractfiles[] = $zip->getNameIndex($i);

            }

        }


foreach($extractfiles as $extractfile) {
     echo $extractfile . "<br>" . PHP_EOL;
}

Upvotes: 1

Related Questions