How to enable ZipArchive for PHP

14Shares

Is your PHP saying that it cannot find the ZipArchive class? If so, then this post will show you how the enable the ZipArchive module. Let fix the “Class ‘ZipArchive’ not found” error!

Continue reading this post if you are seeing the following error:

PHP Fatal error: Uncaught Error: Class 'ZipArchive' not found

Also, below is an example of the code you may be using:

$zip = new \ZipArchive();
$x = $zip->open($target_path);
if ($x === true) {
    $zip->extractTo($target_dir . $name[0]);
    $zip->close();
}

What does this error mean?

This error means that PHP could not find the ZipArchive class. Therefore, PHP needs to have the zip extension installed, for the ZipArchive Class to be present.

You can check if ZipArchive is installed by using a PHP info file.

See an example below:

<?php
phpinfo();
?>

Your PHP info file should return something like this:

Next, scroll down until you find the heading “Configuration” and then continue until you see the following:

You will know that the ZipArchive class is not enabled if you cannot see: “Zip” configuration “enabled”.

Bonus fact! You can also check if the Zip module is enabled using the CLI.

Show compiled in modules using: php -m.

user@server:/var/www/html# php -m
[PHP Modules]
Core
ctype
date
dom
zip
zlib

[Zend Modules]

The PHP module list can get quite large, so feel free to use grep to filter your list:

php -m | grep -i zip

Or, you can use ‘php -i’ to show the PHP information in the CLI. Anto prefers using ‘php -m’.

How to enable ZipArchive on Windows

Windows users have it easy! You do not need to do anything if you are using PHP 5.3 or newer. So, dont trust that code written and tested on Windows will work on Linux.

As of PHP 5.3 this extension is built-in. Before, Windows users need to enable php_zip.dll inside of php.ini in order to use these functions.

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

Before you enable ZipArchive

You can enable Zip support during the installation of PHP as well as after-the-fact. This post will focus on an after-the-fact ZipArchive installation on Linux. Please read the installing Zip instruction from the PHP manual for more information.

Before you continue, note the following usefull command to get your current version of PHP:

user@server:/var/www/html# php -v
PHP 7.4.2 (cli) (built: Feb  1 2020 19:39:30) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

Note the version in the example above being: 7.4.2. We will, therefore, need to install the Zip module for PHP version 7.4. Please remember to change the version number so that it matches yours if the command requires it!

Also, make sure that your packages are up-to-date using the ‘apt-get update’ command:

user@server:/var/www/html# apt-get update
Hit:1 http://security.debian.org/debian-security buster/updates InRelease
Hit:2 http://deb.debian.org/debian buster InRelease
Hit:3 http://deb.debian.org/debian buster-updates InRelease
Reading package lists... Done

Lastly, you may need to determine the location of the loaded php.ini file. Please note that the CLI uses its own version.

Use the following command to help you:

user@server:/var/www/html# php --ini
Configuration File (php.ini) Path: /usr/local/etc/php
Loaded Configuration File:         /usr/local/etc/php/php.ini
Scan for additional .ini files in: /usr/local/etc/php/conf.d
Additional .ini files parsed:      /usr/local/etc/php/conf.d/docker-php-ext-pdo_mysql.ini,
/usr/local/etc/php/conf.d/docker-php-ext-redis.ini,
/usr/local/etc/php/conf.d/docker-php-ext-sodium.ini,
/usr/local/etc/php/conf.d/docker-php-ext-zip.ini

How to enable ZipArchive on Docker

Simply run the following command to install Zip and compile PHP correctly to run it.

apt-get install -y libzip-dev zip && docker-php-ext-install zip

See further below how to restart Apache or NGINX once you install is complete!

Note however that you will get an “E: Package ‘php-XXX’ has no installation candidate” error if you try to install any of Debian’s PHP packages. This is the default intended behaviour of the official PHP Docker image!

You can see an example of this behvaiour below:

user@server:/var/www/html# apt-get install php-zip
Reading package lists... Done
Building dependency tree
Reading state information... Done
Package php-zip is a virtual package provided by:
  php7.3-zip 7.3.14-1~deb10u1 [Not candidate version]
  php7.3-zip 7.3.11-1~deb10u1 [Not candidate version]

E: Package 'php-zip' has no installation candidate

Equally, you can get the following error if you specify a specific verion.

user@server:/var/www/html# apt-get install php7.0-zip
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package php7.0-zip
E: Couldn't find any package by glob 'php7.0-zip'
E: Couldn't find any package by regex 'php7.0-zip'

How to enable ZipArchive on Linux on Ubuntu

The install PHP Zip command:

sudo apt-get install -y php-zip

The output will result in something similar to this:

user@server:/home/user# sudo apt-get install php-zip
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libzip5 php-common php7.3-common php7.3-zip
The following NEW packages will be installed:
  libzip5 php-common php-zip php7.3-common php7.3-zip
0 to upgrade, 5 to newly install, 0 to remove and 48 not to upgrade.
1 not fully installed or removed.
Need to get 1,003 kB of archives.
After this operation, 7,443 kB of additional disk space will be used.

Please note that you may need to specify the PHP version specificaly if you upgraded to version of PHP.

Dont forget to restart your web server!

The configuration file (php.ini) is only read when PHP or when the webserver is started. Equally, the php.ini file is only read at invocation for the CGI and CLI versions.

You can restart Apache using:

service apache2 restart

You can resart NGINX using:

service nginx restart

Consluion

It does not take much effort to get your website zipping files, and avoid a fatal error! Hopefully, you now know how to enable ZipArchive for PHP. No one likes seeing a “Fatal error: Class ‘ZipArchive’ not found in”. Contact Anto here if you have some advice you want to share.

Hi, my name is Anto! I am a cloud computing hobbyist! Give me anything to do with the cloud, and I am interested. I work for a Cloud computing company by day and as a Cloud computing hobbyist by night! My projects use PHP, NodeJs, Ubuntu, MySQL and of course Amazon Web Services. Hopefully, my blog aids your cloud journey! Feel free to post a comment and share your thoughts.

Leave a Reply

Your email address will not be published. Required fields are marked *