236

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.

Alessio Cantarella
  • 5,077
  • 3
  • 27
  • 34
Parag Chaure
  • 2,883
  • 2
  • 19
  • 27

29 Answers29

267

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).

On Debian and Ubuntu, you can usually install it with:

sudo apt update
sudo apt install php-zip

Then restart your webserver. Example:

sudo systemctl restart apache2
Valerio Bozz
  • 1,176
  • 16
  • 32
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • I have installed Archive_Tar and File_Archive extensions on server – Parag Chaure Oct 06 '10 at 13:01
  • @parag then those aren't the right extensions, are they? The one in question here seems to be named "zip" – Pekka Oct 06 '10 at 13:05
  • I search on google and put these extensions to server. This is the first time I'm using the ziparchive or creating the zip file, So I'm not sure that are they right extensions. – Parag Chaure Oct 06 '10 at 13:13
  • @parag see the page with installation instructions I linked to – Pekka Oct 06 '10 at 13:15
  • 1
    @parag you will need to equip your server with the Zip extension. It's probably already installed on your local PC because you're using a development package like XAMPP or WAMP - those already come with the extension installed. – Pekka Oct 06 '10 at 13:25
  • @parag if you have no root access to your server, you will have to ask the administrator to install the extension. – Pekka Oct 06 '10 at 13:26
  • which extension is to be installed? can you please tell me the name of extension – Parag Chaure Oct 06 '10 at 13:44
  • @parag it depends on the machine. On Linux, I think this is the way to go: `In order to use these functions you must compile PHP with zip support by using the --enable-zip configure option.` – Pekka Oct 06 '10 at 13:45
  • 1
    According to the PHP manual, ZIPARCHIVE is available from version 5.2.0 of PHP. – Wallace Vizerra Jan 22 '15 at 11:24
  • 60
    On ubuntu server using apt-get `sudo apt-get install php7.0-zip ` – Luddig Jul 25 '16 at 23:28
  • 9
    Simple solution: `sudo apt-get install php7.0-zip`. Then, edit the file `/etc/php/7.0/cli/php.ini` (In the "Dynamic Extensions" section, add the line `extension=zip.so`). This should solve it – JonyD Sep 02 '16 at 12:05
  • 26
    For Docker `docker-php-ext-install zip` – David Riccitelli Nov 10 '16 at 14:06
  • 14
    As mentioned by @Luddig on ubuntu server after `sudo apt-get install php7.0-zip` you will need to restart the apache server `sudo systemctl restart apache2` – hashmi May 25 '17 at 10:25
  • On Jessie linux `apt-get install -y libzip-dev` and `pecl install zip`, after which the extension `zip.so` has to be enabled in the `php.ini` file and apache restarted. However, when using docker, @DavidRiccitelli has the correct answer. – kregus Sep 13 '18 at 10:09
94

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

sudo apt-get install php7.0-zip

DPP
  • 12,716
  • 3
  • 49
  • 46
43

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.

Mubashar Abbas
  • 5,536
  • 4
  • 38
  • 49
25

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
lorenzo-s
  • 16,603
  • 15
  • 54
  • 86
23

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.

Kalyan Halder
  • 1,485
  • 24
  • 28
  • This worked for me. I was using a template builder and it would not export. I checked the zip checkbox, saved and voila. Thank you Kaylan – Hblegg May 13 '18 at 06:02
  • Have been trawling the net for hours for a solution, this was spot on for me! Thank you good sir! – ChrVik Nov 08 '22 at 21:17
10

Centos 6

Or any RHEL-based flavors

yum install php-pecl-zip

service httpd restart
Abel Callejo
  • 13,779
  • 10
  • 69
  • 84
zzapper
  • 4,743
  • 5
  • 48
  • 45
  • 1
    Thank you. I have installed php-pecl-zip, but without restarting httpd. Finally found your answer! – Ngoc Nam Jun 01 '19 at 16:30
8

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.

temo
  • 612
  • 1
  • 9
  • 25
6

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
Santo Boldizar
  • 1,255
  • 14
  • 17
5

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.

Jeremy
  • 2,651
  • 1
  • 21
  • 28
  • yes, but where should I run this commands on the server so that php runs with zip support – Parag Chaure Oct 06 '10 at 13:15
  • Is it a server that you control? If so I believe it's part of the make command when installing PHP. If it's not a server that you control then the only option for you is to try to have the sys admin do it. Sorry I can't be more precise-I've never installed PHP on a linux server from scratch. – Jeremy Oct 06 '10 at 13:22
  • I can confirm. Installing all those packages did not work. I had to ./configure PHP again with the `--enable-zip` option and make && make install again (PHP 7.4.8 debian). – Mtxz Aug 07 '20 at 21:47
5

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

Alessio Cantarella
  • 5,077
  • 3
  • 27
  • 34
prosti
  • 42,291
  • 14
  • 186
  • 151
5

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. 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.

Anton Perera
  • 353
  • 3
  • 9
4

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.

V A S
  • 3,338
  • 4
  • 33
  • 39
4

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
kiatng
  • 2,847
  • 32
  • 39
3

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
Omar Shabab
  • 531
  • 7
  • 16
3

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
Doberon
  • 611
  • 9
  • 19
2

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.

Capripot
  • 1,354
  • 16
  • 26
2

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

alex toader
  • 2,300
  • 1
  • 17
  • 20
1

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

Wolfack
  • 2,667
  • 1
  • 26
  • 50
1

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

Sundar
  • 253
  • 2
  • 6
0

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

AboElnouR
  • 281
  • 3
  • 14
0

Try to write \ZIPARCHIVE instead of ZIPARCHIVE.

URL87
  • 10,667
  • 35
  • 107
  • 174
0

namespace

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

$zip = new \ZipArchive;
Mahdi Bashirpour
  • 17,147
  • 12
  • 117
  • 144
0
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

Sonu Chohan
  • 141
  • 1
  • 5
0

The same in CMS Typo3 ver.11.5.8 while installing an extension by the Extension Manager, nevermind that the Zip extension was installed in the cpanel.

The reason: in the .htaccess there was:

AddType application/x-httpd-php74 .php AddHandler application/x-httpd-php74 .php

The fix: change the above by: AddType application/x-httpd-alt-php74 .php .php7 .phtml

Now it's working in me.

0

Don't forget to use this class use ZipArchive;

0
  • Goto php.ini
  • Search for zip
  • Remove ';' from that line (uncomment it)
0

Under Windows

As of PHP 8.2.0, php_zip.dll DLL must be enabled in php.ini. Previously, this extension was built-in.

source: https://www.php.net/manual/en/zip.installation.php

Zsolti
  • 1,571
  • 1
  • 11
  • 22
0

You can use the following script to find the currently installed PHP versions:

# /usr/local/cpanel/bin/rebuild_phpconf --current

Once you have identified that version of PHP, you can then install the missing "Zip" module. You can install them with the following command:

yum install ea-phpXX-php-zip*
toyota Supra
  • 3,181
  • 4
  • 15
  • 19
-13

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

Alex Pliutau
  • 21,392
  • 27
  • 113
  • 143