Installing the PHP 7 Zip extension on CentOS 7 can involve several steps, but this article will show you the easiest way along with some useful tips.
First, you can check if the Zip extension is already installed by running the following command:
php -m | grep zip
If you see "zip" in the output, it means the extension is already installed, and no further action is needed.
If the Zip extension is not installed, you can install it using the YUM package manager:
<span class="fun">yum install php-zip</span>
After installation, you will need to restart the Apache server to enable the Zip extension:
<span class="fun">service httpd restart</span>
If you are using Nginx, restart it with the following command:
<span class="fun">service nginx restart</span>
After installing the Zip extension, you can use the phpinfo() function to verify the installation.
Create a test.php file with the phpinfo() function and place it in the root directory of your Apache server:
<?php
phpinfo();
?>
Access this file and look for the "zip" extension in the output to confirm that it is successfully installed.
Once the Zip extension is enabled, you can use it in your PHP scripts to create and manipulate Zip files. Here is an example script to create a Zip file:
<?php
$zip = new ZipArchive();
$filename = "/tmp/test.zip";
if ($zip->open($filename, ZipArchive::CREATE) !== TRUE) {
exit("Failed to create file $filename\n");
}
$zip->addFromString("testfile.txt", "This is test text for the ZIP file");
echo "Number of files: " . $zip->numFiles . "\n";
echo "Status: " . $zip->status . "\n";
$zip->close();
?>
This script creates a test.zip file and adds a file called testfile.txt to it.
This article has provided the simplest method for installing the PHP 7 Zip extension on CentOS 7. After installation, remember to restart Apache or Nginx to activate the Zip extension. We hope this guide has helped you set up and start using the Zip extension on your PHP setup.