开发者

PHP Zip Creating Class ZipArchive Not Found Error

开发者 https://www.devze.com 2023-02-16 20:54 出处:网络
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.

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开发者_StackOverflow中文版 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


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


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.


You could try dl("zip.so"); as last resort. But that extension is rarely built as external module. You might be able to find it downloadable somewhere, or even compile it by hand from the PHP sources or http://pecl.php.net/package/zip using pecl build

Otherwise you will have to look for an alternative. http://pear.php.net/package/Archive_Zip comes to mind.


If you have upgrade php version or zip extension is not there

So you have to install it .

1) Open terminal

2) sudo apt-get install php7.0-zip

3) sudo service apache2 restart

0

精彩评论

暂无评论...
验证码 换一张
取 消