Fatal error: Class 'ZipArchive' not found in

PhpZipZiparchive

Php Problem Overview


I have a problem that I install 'Archive_Zip 0.1.1' on Linux server, but when I try to run the script to create the zip file it gives the fatal error

> Fatal error: Class ZipArchive not found in ...

where I put the code

$zip = new ZipArchive;
var_dump($zip);
$res = $zip->open($filename, ZipArchive::OVERWRITE);
if ($res !== TRUE) {
	echo 'Error: Unable to create zip file';
	exit;
}
if (is_file($src)) {
	$zip->addFile($src);
} else {
	// echo "<br>" . dirname(__FILE__) . $src;//'/install1';
	if (!is_dir($src)) {
		 $zip->close();
		 @unlink($filename);
		 echo 'Error: File not found';
		 exit;
	}
	recurse_zip($src, $zip, $path_length);
}
$zip->close();
echo "<br>file name ".$filename;

but it doesn't find the class file.

Please tell me the solution. What should I do to resolve the problem? I also put php.ini file to the folder where script is, but it does not work.

Php Solutions


Solution 1 - Php

For the ZipArchive class to be present, PHP needs to have the zip extension installed.

See this page for installation instructions (both Linux and Windows).

Solution 2 - Php

On Amazon ec2 with Ubuntu + nginx + php7, I had the same issues, solved it using:

Solution 3 - Php

On ubuntu desktop, I had to do.

sudo apt-get install php5.6-zip

This installed the library but I still kept on getting the same error, so I had to restart apache using:

sudo service apache2 restart

and it worked.

Solution 4 - Php

I'm not seeing it here, so I'd like to add that on Debian/Ubuntu you may need to enable the extension after installing the relative package. So:

sudo apt-get install php-zip
sudo phpenmod zip
sudo service apache2 restart

Solution 5 - Php

First of all, The solution for remote server:

If you are using cpanel you may have zip extension installed but not activate. You need to active it. For this case you need to go to cpanel > inside software section > click on PHP version. Then find zip and check it. Now save.

You should see like the image. enter image description here

Refresh page. The error should disappear.

Note: If you dont found, contact server provider. They will install for you.

Solution 6 - Php

Centos 6

Or any RHEL-based flavors

yum install php-pecl-zip

service httpd restart

Solution 7 - Php

If you have WHM available it is easier.

Log in to WHM.

Go to EasyApache 4 (or whatever version u have) under Software tab.

Under Currently Installed Packages click Customize.

Go to PHP Extensions, in search type "zip" (without quotes),

you should see 3 modules

check all of them,

click blue button few times to finish the process.

This worked for me. Thankfully I've WHM available.

Solution 8 - Php

You also need to compile PHP with zip support. The manual says the following:

> In order to use these functions you must compile PHP with zip support by using the --enable-zip configure option.

It's not enough to simply install the correct extensions on the server. Have a look at the installation instructions link Pekka posted earlier. My answer is just a clarification of his.

Solution 9 - Php

This worked

apt-get install php7.0-zip

and no need to restart php7.0-fpm manually.

> Unpacking php7.0-zip (7.0.16-4+deb.sury.org~trusty+1) ...
Processing triggers for php7.0-fpm (7.0.11-1+deb.sury.org~trusty+1) ...
php7.0-fpm stop/waiting
php7.0-fpm start/running, process 1572
php7.0-fpm stop/waiting
php7.0-fpm start/running, process 1777
Setting up php7.0-zip (7.0.16-4+deb.sury.org~trusty+1) ...
locale: Cannot set LC_ALL to default locale: No such file or directory

> Creating config file /etc/php/7.0/mods-available/zip.ini with new version
Processing triggers for php7.0-fpm (7.0.11-1+deb.sury.org~trusty+1) ...
php7.0-fpm stop/waiting
php7.0-fpm start/running, process 2354
php7.0-fpm stop/waiting
php7.0-fpm start/running, process 2397

Solution 10 - Php

I had the same issue with CentOS and cPanel installed server. I installed zipArchive package via cPanel and didn't worked as expected. After trying with so many fixes suggested each and everywhere just the below worked for me.

First find the name for the correct package with the below command

yum search zip |grep -i php

Then use the below code.

yum install your_zip_package_name_with_php_version

In my case correct code to install zipArchive was

yum install php-pecl-zip.x86_64

I had the solution from this link. https://stackoverflow.com/questions/49583881/how-can-i-inslatt-ziparchive-on-php-7-2-with-centos-7

And this installation somehow enabled that package too and it also restarted the effecting services and after the completion of the execution of the above code zipArchive issue was gone.

Solution 11 - Php

For PHP 7.x

sudo apt-get install php-zip

For PHP 5.x

sudo apt-get install php5.x-zip
// (for example sudo apt-get install php5.6-zip)

And then restart the Apache server

sudo service apache2 restart

Solution 12 - Php

For Centos 7 and PHP 7.3 on Remi

Search for the zip extension:

$ yum search php73 | grep zip
php73-php-pecl-zip.x86_64 : A ZIP archive management extension

The extension name is php73-php-pecl-zip.x86_64. To install it in server running single version of PHP, remove the prefix php73:

$ sudo yum --enablerepo=remi-php73 install php-pecl-zip #for server running single PHP7.3 version
$ #sudo yum --enablerepo=remi-php73 install php73-php-pecl-zip # for server running multiple PHP versions

Restart PHP:

$ sudo systemctl restart php-fpm

Check installed PHP extensions:

$ php -m
[PHP Modules]
apcu
bcmath
bz2
...
zip
zlib

Solution 13 - Php

PHP 5.2.0 and later

Linux systems

In order to use these functions you must compile PHP with zip support by using the --enable-zip configure option.

Windows

Windows users need to enable php_zip.dll inside of php.ini in order to use these functions.

Solution 14 - Php

I faced this issue on GCP while deploying wordpress in the App Engine Standard environment. This solved it :

sudo apt-get install php7.2-zip

Solution 15 - Php

For me work, first review

   php -m
   php -version

Later install the extension

apt-get update 
apt-get install php7.2-zip
systemctl restart apache2

Solution 16 - Php

For PHP 7.4 > The ZipArchive is not longer present by default and you need to install and configure it.

I strongly suggest using a library which you can just include in your project - like https://github.com/zanysoft/laravel-zip This library is just a wrapper for the native ZipArchive

Solution 17 - Php

For CentOS based server use

yum install php-pecl-zip.x86_64

Enable it by running: echo "extension=zip.so" >> /etc/php.d/zip.ini

Solution 18 - Php

You need to check the PHP version

If php version is 5.6 then , You need to install php5.7-zip

sudo apt-get install php5.6-zip

and then

sudo service apache2 restart

Hope it helps

Solution 19 - Php

If you are running into this error while using a Docker image and that you are calling the class properly, or that the error comes from an up-to-date library, then the zip module is probably missing.

Assuming we use docker-compose, we can confirm it's missing by running docker-compose run php php -m for instance and see that zip is not listed.

To install it in your image, modify your Dockerfile so it does the same as this example.

FROM php:7.3-apache 

RUN set -eux \
    && apt-get update \
    && apt-get install -y libzip-dev zlib1g-dev \
    && docker-php-ext-install zip

Then rebuild the image with docker-compose build php and you are good to go.

Solution 20 - Php

I had the same issue and it had solved using two command lines:

sudo apt install php-zip

then reboot your web server, for Apache

sudo service apache2 restart

Solution 21 - Php

Try to write \ZIPARCHIVE instead of ZIPARCHIVE.

Solution 22 - Php

namespace

If you get this problem after adding namespace you can use the global namespace (backslash \ ):

$zip = new \ZipArchive;

Solution 23 - Php

CPanel [ Sharing Hosting ]
To fix this issue or error use the below steps:-

Error Details:-
/=========================================================================
An uncaught Exception was encountered
Type: Error

Message: Class 'ZipArchive' not found

Filename: /home/b2bvniowy1go/public_html/system/libraries/PHPExcel/Reader/Excel2007.php

Line Number: 85

Backtrace:

File: /home/b2bvniowy1go/public_html/dashboard/controllers/Brand_price.php

============================================================================ /

cPanel Version [ 94.0 (build 19) ]
Solution:-  Do this changes to your remote server:  Cpanel > Select PHP version section > extensions tab. Then find zip and check it. Now save.

zip setting in cpanel

Solution 24 - Php

  1. You should require your file with ZipArchive file.

    require 'path/to/file/ZipArchive.php';

  2. Or use __autoload method of class. In PHP 5 it is a greate method __autoload().

    function __autoload($class_name) { require_once $class_name . '.php'; }

    $obj = new MyClass1(); // creating an object without require. http://www.php.net/manual/en/language.oop5.autoload.php

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionParag ChaureView Question on Stackoverflow
Solution 1 - PhpPekkaView Answer on Stackoverflow
Solution 2 - PhpDPPView Answer on Stackoverflow
Solution 3 - PhpMubashar AbbasView Answer on Stackoverflow
Solution 4 - Phplorenzo-sView Answer on Stackoverflow
Solution 5 - PhpKalyan HalderView Answer on Stackoverflow
Solution 6 - PhpzzapperView Answer on Stackoverflow
Solution 7 - PhptemoView Answer on Stackoverflow
Solution 8 - PhpJeremyView Answer on Stackoverflow
Solution 9 - PhpprostiView Answer on Stackoverflow
Solution 10 - PhpantonDView Answer on Stackoverflow
Solution 11 - PhpSanto BoldizarView Answer on Stackoverflow
Solution 12 - PhpkiatngView Answer on Stackoverflow
Solution 13 - PhpVineesh KalarickalView Answer on Stackoverflow
Solution 14 - PhpOmar ShababView Answer on Stackoverflow
Solution 15 - PhpDoberonView Answer on Stackoverflow
Solution 16 - Phpalex toaderView Answer on Stackoverflow
Solution 17 - PhpWolfackView Answer on Stackoverflow
Solution 18 - PhpSundarView Answer on Stackoverflow
Solution 19 - PhpCapripotView Answer on Stackoverflow
Solution 20 - PhpAboElnouRView Answer on Stackoverflow
Solution 21 - PhpURL87View Answer on Stackoverflow
Solution 22 - PhpMahdi BashirpourView Answer on Stackoverflow
Solution 23 - PhpSonu ChohanView Answer on Stackoverflow
Solution 24 - PhpAlex PliutauView Answer on Stackoverflow