Fatal error: Class 'ZipArchive' not found in
When you encounter a Fatal error: Class 'ZipArchive' not found on a Linux server, the immediate assumption is often that a missing file or configuration issue is to blame. However, PHP's built-in ZipArchive class is part of the core extension set and does not require external installation like third-party libraries such as Archive_Zip. The error message you are seeing indicates that the PHP runtime cannot locate this specific extension within its current environment.
This discrepancy usually stems from one of three scenarios: a mismatch between the compiled PHP binary and the available extensions, an incorrect configuration path in php.ini, or running the script under a context (like CGI/FastCGI) where the extension is not loaded.
Understanding the Extension Mismatch
The most common root cause on Linux systems is that the PHP binary executing your script was built without the zip module enabled, or it was built against a different set of libraries than what you think is installed. For instance, if you installed PHP via a package manager like apt or yum, and then separately compiled an extension from source (or vice versa), they might not be speaking the same language.
The Archive_Zip 0.1.1 library mentioned in your scenario is a third-party dependency that relies on the system's native PHP ZipArchive class to function. It does not replace the core class; it wraps it. If the core class is missing, the wrapper will fail immediately upon instantiation with the error you observed.
Verifying Extension Availability
Before modifying configuration files, verify whether the extension is actually compiled into your PHP binary. Run the following command in your terminal:
php -m | grep zip
If this returns nothing, the extension is not loaded. You might see zip listed if it is enabled, but the error still occurs if you are running a specific SAPI (Server API) or user context where modules are restricted.
Correctly Configuring php.ini
You mentioned placing the php.ini file in the folder containing the script. This is a frequent mistake. PHP does not automatically scan the current working directory for configuration files. It looks for php.ini in specific system paths defined by the PHPRC environment variable or within the installation directory itself (often /etc/php/<version>/cli/ or /etc/php/<version>/fpm/).
To resolve this:
- Locate the active
php.iniused by your server. You can find the exact path by running:php --ini - Open that specific file and ensure the following line is uncommented (remove the leading semicolon):
extension=zip - Save the file and restart your web server or PHP-FPM process to apply changes.
Do not assume that placing a config file in the project root will override the system configuration unless you explicitly set php.ini at the top of your php.ini master file to point to your custom location, which is rarely necessary for standard deployments.
Troubleshooting Compiled vs. Package Manager Issues
If you are on a Linux distribution like Ubuntu or CentOS and have installed PHP via apt-get install php-cli, but then tried to add the zip extension manually, you may be conflicting with the system's package management strategy. The built-in PHP usually comes with common extensions pre-enabled. If the module is missing, it is likely that the specific PHP build you are using was compiled without it.
In such cases, the most robust solution is to reinstall or recompile PHP with the --with-zip flag included, or ensure your package manager's repository includes the necessary dependencies:
# Example for Debian/Ubuntu systems
sudo apt-get install php-zip
sudo systemctl restart apache2 # Or nginx/fpm depending on setup
Code-Specific Considerations
Your script snippet creates a ZipArchive object directly. This instantiation happens before any logic can run, meaning the fatal error occurs at line 1 of your execution flow:
$zip = new ZipArchive;
Because this is a core class, there are no alternative libraries to "install" that will magically make it appear unless the underlying PHP binary supports it. The Archive_Zip library you attempted to install expects this class to exist natively. If your environment lacks the php-zip PECL extension or built-in module, the application cannot function regardless of how the logic is structured afterward.
Ensure that the user running your web server (e.g., www-data) has permission to read the configuration files and that the PHP-FPM pool or Apache virtual host is not stripping extensions for security reasons, though this is less common with core modules like ZipArchive.
By aligning your PHP build environment with the required extensions and pointing to the correct system-wide configuration file, you eliminate the mismatch causing the fatal error.