Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Zip Creating Class ZipArchive Not Found Error

Tags:

php

zip

zlib

i'm trying to create a zip script based on what I've found here but I seem to be getting a Fatal error: Class 'ZipArchive' not found error on the new ZipArchive(); function.

Researching this it seems that this is usually due to the way PHP is compiled. I have a shared hosting account, so i've not configured any of this stuff...and I assume that I can't make any changes to the build. Out of interest I took a look in my phpinfo() and I found some things that looked associated:

PHP Version 5.2.6

BZip2 Support   Enabled    <--maybe not so relevant
ZLib Support    enabled
Stream Wrapper support  compress.zlib://
Stream Filter support   zlib.inflate, zlib.deflate
Compiled Version    1.1.4
Linked Version  1.1.4 

I'm not entirly sure if any of this means that I have the ability to create zips. For further info (although I don't think it's relivent) here's my script so far....this is untested mind you as I can't get pased this Class not found error.

$file = tempnam("tmp", "zip");
$zip = new ZipArchive();
$zip->open($file, ZipArchive::OVERWRITE);


   //the string "file1" is the name we're assigning the file in the archive
$zip->addFile('show1.jpg', 'file1.jpg');
$zip->addFile('show2.jpg', 'file2.jpg');
$zip->addFile('show3.jpg', 'file3.jpg');
$zip->addFile('show4.jpg', 'file4.jpg');
$zip->addFile('show5.jpg', 'file5.jpg');
$zip->addFile('show6.jpg', 'file6.jpg');

// echo $zip->file(); //this sends the compressed archive to the output buffer instead of writing it to a file.

$zip->close();
header('Content-Type: application/zip');
header('Content-Length: ' . filesize($file));
header('Content-Disposition: attachment; filename="' . $file.'"');
readfile($file);
unlink($file); 

So my question(s) really are:

  1. Am I doing anything in my script to cause this error?
  2. Does any of that stuff from my phpinfo() mean I should be able to create zip files, ..if not what should I be looking for in there that will let me know if i have the capability.
  3. It looks like this ZLib is some soft of library, but I've got no idea if it does what I want it do, or even how to use it....this is a bit of a hunch, but if it can help me create zip files can anyone point me in the right direction of how to use it?

Thanks in advance. Dan

like image 675
Dan Twining Avatar asked Oct 12 '22 13:10

Dan Twining


2 Answers

ZipArchive is apparently not compiled into PHP by default. You need to either recompile it with the '--with-zip=' option or simply install it via PECL.

Here is the manual page explaining the different methods:

http://www.php.net/manual/en/zip.installation.php

like image 196
vicTROLLA Avatar answered Nov 17 '22 22:11

vicTROLLA


While zlib is an important compression library, it sounds like you're missing the zip extension itself. It looks like you got your information from phpinfo -- look for the exact words "zip extension." If you can't find them, you don't have it installed, and thus can not use the functions and methods provided by it.

like image 20
Charles Avatar answered Nov 17 '22 21:11

Charles