When it comes to ZIP archive and file manipulation most of the modern programming languages provide some kind of support. PHP is not an exception and the PHP community provides the ZIP extension.
Similarly to other PHP extensions, the ZIP extension needs to be installed on the server. The installation process varies depending on the server platform but it is easy and quite documented.
Having installed the Zip extension your code now has access to the ZipArchive class which can be used for everything Zip related.
But at first lets inform our dependency manager Composer that we are using the extension, thus the IDE will recognize the extension, plus we will get an early error in case the extension is not present on the server.
// composer.json
"require": {
"ext-zip": "*",
},
As of PHP 8 the procedural Zip functions are deprecated, so just use the ZipArchive class.
The ZipArchive class is our gateway to the functionalities provided by the Zip extension.
In order to create a zip archive we can use the open method, which takes two arguments, the filename of the new archive and the mode
$zip = new ZipArchive();
$zip->open($filename, ZipArchive::CREATE);