3

I have a little php, application or whatever that opens a zip and reads the content. But it only works SOME of the time... sometimes when I upload the .zip and try to view contents, it works and echos back each file to me, but some other times ( yes i have a lot of .zip files ), it returns these errors:

Warning: zip_read() expects parameter 1 to be resource, integer given in /home/blah/public_html/templates.php on line 23

Warning: zip_close() expects parameter 1 to be resource, integer given in /home/blah/public_html/templates.php on line 31

Here is my code:

$open = zip_open($file);
while($zip = zip_read($open)) {
$file = zip_entry_name($zip);
echo $file.'<br />';
}
zip_close($open);
Dharman
  • 30,962
  • 25
  • 85
  • 135
David
  • 2,365
  • 8
  • 34
  • 59

2 Answers2

8

The cases where that happens are cases when the Zip file can not be opened.

Zip_open() returns an integer instead of the file handle when it encounters an error. Documentation

Returns a resource handle for later use with zip_read() and zip_close() or returns the number of error if filename does not exist or in case of other error.

You need to output $open and check what error code it gives you. You should build this into the code as a fixed check before trying to run any zip operations.

This table will tell you which error code means what.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
2
$open = zip_open($file);

if (is_numeric($open)) {
    echo "Zip Open Error #: $open";
   } else {
   while($zip = zip_read($open)) {
   .....
   }
Anthony
  • 36,459
  • 25
  • 97
  • 163