Aniket Singh
Aniket Singh

Reputation: 877

How to check if zip file contains all images file without unzipping on server

I want to check if all files in zip are images or not, till now i have come up with this solution

$zip = new ZipArchive;
$res = $zip->open('CQN.zip');

if ($res) {

    $legitImage=explode('.',$zip->statIndex(0)['name']);
    if($legitImage[1] !='jpg')
    {
        // just stop processing
    }
}

I just want to loop every file in zip for images, if image is not found than just echo the error

Upvotes: 0

Views: 674

Answers (1)

b2ok
b2ok

Reputation: 536

<?php
$za = new ZipArchive(); 

$za->open('theZip.zip'); 

for( $i = 0; $i < $za->numFiles; $i++ ){ 
    $stat = $za->statIndex( $i ); 

     $ext = pathinfo(( basename( $stat['name'] ) . PHP_EOL ), PATHINFO_EXTENSION);

     echo $ext;
     echo "<br>";
}
?>

This I'm adding array of $ext from FOR loop, take array ouside loop and you can manipulate with that array depend what you wont.

<?php
$array = array();


$za = new ZipArchive(); 

$za->open('theZip.zip'); 

for( $i = 0; $i < $za->numFiles; $i++ ){ 

   $stat = $za->statIndex( $i ); 

     $ext = pathinfo(( basename( $stat['name'] ) . PHP_EOL ), PATHINFO_EXTENSION);

     $array[] = $ext;

}
print "For: ".count($array)."<br />";

print_r($array);

foreach ($array as $value) {
    echo $value . "<br />";

}

?>

Upvotes: 1

Related Questions