Fatal error: Class 'ZipArchive' not found in
Asked Answered
E

23

247

I have a problem when I install 'Archive_Zip 0.1.1' on the 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 the php.ini file in the folder where the script is, but it does not work.

Ergocalciferol answered 6/10, 2010 at 12:47 Comment(2)
You can view this post: https://mcmap.net/q/118894/-ziparchive-does-not-work-in-laravelInfix
For PHP 8 and 7.4 the extension is no longer libzip. You compile PHP with --with-zip parameter now. And don't forget to restart Apache afterwards.Lorie
B
282

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
Backstroke answered 6/10, 2010 at 12:49 Comment(14)
I have installed Archive_Tar and File_Archive extensions on serverErgocalciferol
@parag then those aren't the right extensions, are they? The one in question here seems to be named "zip"Backstroke
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.Ergocalciferol
@parag see the page with installation instructions I linked toBackstroke
@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.Backstroke
@parag if you have no root access to your server, you will have to ask the administrator to install the extension.Backstroke
which extension is to be installed? can you please tell me the name of extensionErgocalciferol
@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.Backstroke
According to the PHP manual, ZIPARCHIVE is available from version 5.2.0 of PHP.Myca
On ubuntu server using apt-get sudo apt-get install php7.0-zip Protect
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 itPavior
For Docker docker-php-ext-install zipRudolphrudwik
As mentioned by @Protect on ubuntu server after sudo apt-get install php7.0-zip you will need to restart the apache server sudo systemctl restart apache2Crank
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.Buddy
A
95

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

sudo apt-get install php7.0-zip

Axel answered 6/5, 2016 at 6:56 Comment(3)
after apt-get install php7.0-xsl got the problem ... don't worry just apt-get install php7.0-zip to solveAmmon
sudo apt-get install php7.1-zip for Ubuntu/php7.1Ide
this does not work with php 7.2 and always says E: Unable to locate package php7.2-zip E: Couldn't find any package by regex 'php7.2-zip'Lopez
A
46

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.

Anoa answered 28/10, 2016 at 12:10 Comment(1)
The restarting apache did it, when combined with @pekka's solutionDixson
S
27

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
Sporophyte answered 16/10, 2017 at 6:54 Comment(4)
Install php7.0-zip for PHP 7.Indisposition
DON'T FORGET THE APACHE RESTART!!Nehemiah
Install php7.1-zip for PHP 7.1, php7.2-zip for PHP 7.2.Scraggy
As far as I know there is no need to specify the PHP version. "sudo apt-get install php-zip" will select your system PHP version and was sufficient when installing for php7.3Stefanistefania
E
25

If you are using cPanel you may have zip extension installed but not activated. You need to activate it. For this case you need to go to cpanel > inside the 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 don't find it, contact the server provider. They will install it for you.

Exorable answered 17/11, 2017 at 2:39 Comment(2)
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 KaylanDeciduous
Have been trawling the net for hours for a solution, this was spot on for me! Thank you good sir!Enrico
N
11

Centos 6

Or any RHEL-based flavors

yum install php-pecl-zip

service httpd restart
Nehemiah answered 10/10, 2018 at 13:57 Comment(1)
Thank you. I have installed php-pecl-zip, but without restarting httpd. Finally found your answer!Unmanned
R
9

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.

Refugio answered 15/5, 2018 at 11:36 Comment(1)
After choosing the 3 zip extensions, Go to "Review" then click on "Provision" then click "Done" on the next pagePlover
K
7

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
Ketubim answered 6/12, 2019 at 5:32 Comment(0)
A
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.

Attending answered 2/9, 2018 at 9:52 Comment(0)
M
4

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.

Madewell answered 6/10, 2010 at 13:8 Comment(3)
yes, but where should I run this commands on the server so that php runs with zip supportErgocalciferol
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.Madewell
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).Geometrize
M
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
Mutter answered 7/1, 2020 at 3:51 Comment(0)
A
3

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.

Amphiprostyle answered 1/7, 2013 at 11:39 Comment(0)
C
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.

Casaleggio answered 9/12, 2020 at 8:43 Comment(0)
G
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

Gallonage answered 15/9, 2021 at 8:13 Comment(0)
M
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

Mcelrath answered 7/1, 2020 at 11:10 Comment(0)
S
0

Try to write \ZIPARCHIVE instead of ZIPARCHIVE.

Stakeout answered 9/2, 2020 at 12:27 Comment(0)
P
0

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

$zip = new \ZipArchive;
Palazzo answered 18/6, 2021 at 22:2 Comment(0)
S
0
  • Goto php.ini
  • Search for zip
  • Remove ';' from that line (uncomment it)
Slimsy answered 23/5, 2023 at 9:41 Comment(0)
B
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*
Bloodless answered 9/8, 2023 at 14:38 Comment(0)
C
0

update for newer versions of php, the extension file is ';extension=zip' and not the complete 'php_zip.dll' file name. just remove the semicolon from the start and this should do the trick.

Cullie answered 6/3, 2024 at 13:10 Comment(0)
L
-1

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.

Laine answered 7/2, 2023 at 22:38 Comment(0)
S
-1

Solution for xampp users only

If you are getting Class "ZipArchive" not found error message in Laravel then make sure following 4 things:

  1. zip extension is enabled or not in the xampp, check it by phpinfo() function.

  2. You are running correct php version in xampp.

  3. You have set correct same php version folder path in path in Environment Variable on your windows system.

  4. In the command line, first make sure correct php version by php -v command. If php version is correct then run php artisan serve command in CLI/command line console.

I am sure, this will solve your problem immediately. Happy to help you :)

Shelbashelbi answered 13/2, 2024 at 11:28 Comment(1)
Why is this for XAMPP users only? PHP is PHP regardless of how it was installed.Hurff
F
-14

If you have defined a ZipArchive in your own namespace, you can try either of these steps:

  1. You should require your file with ZipArchive file.

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

  2. Or use autoloading to automatically require the custom class.

Foumart answered 6/10, 2010 at 12:52 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.