How to Create a ZIP File in PHP

Cloudmersive
2 min readApr 3, 2024

--

We can rapidly compress multiple files into a new ZIP archive using just a few lines of PHP code.

We’ll make this process extremely easy by calling a free API to handle the archive compression process.

To call this API, we can start by installing the client SDK. Let’s execute this command in our command line to install with Composer:

composer require cloudmersive/cloudmersive_document_convert_api_client

Next, lets’ copy the below code to call the function. We can compress 10+ file paths through the $input_file variables:

<?php
require_once(__DIR__ . '/vendor/autoload.php');

// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');



$apiInstance = new Swagger\Client\Api\ZipArchiveApi(


new GuzzleHttp\Client(),
$config
);
$input_file1 = "/path/to/inputfile"; // \SplFileObject | First input file to perform the operation on.
$input_file2 = "/path/to/inputfile"; // \SplFileObject | Second input file to perform the operation on.
$input_file3 = "/path/to/inputfile"; // \SplFileObject | Third input file to perform the operation on.
$input_file4 = "/path/to/inputfile"; // \SplFileObject | Fourth input file to perform the operation on.
$input_file5 = "/path/to/inputfile"; // \SplFileObject | Fifth input file to perform the operation on.
$input_file6 = "/path/to/inputfile"; // \SplFileObject | Sixth input file to perform the operation on.
$input_file7 = "/path/to/inputfile"; // \SplFileObject | Seventh input file to perform the operation on.
$input_file8 = "/path/to/inputfile"; // \SplFileObject | Eighth input file to perform the operation on.
$input_file9 = "/path/to/inputfile"; // \SplFileObject | Ninth input file to perform the operation on.
$input_file10 = "/path/to/inputfile"; // \SplFileObject | Tenth input file to perform the operation on.

try {
$result = $apiInstance->zipArchiveZipCreate($input_file1, $input_file2, $input_file3, $input_file4, $input_file5, $input_file6, $input_file7, $input_file8, $input_file9, $input_file10);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling ZipArchiveApi->zipArchiveZipCreate: ', $e->getMessage(), PHP_EOL;
}
?>

Finally, let’s grab a free Cloudmersive API key to authorize our API calls. We can copy this into$config, and it’ll allow us a limit of 800 API calls per month with no commitments.

All done! Now we can quickly and easily compress files into ZIP archives using minimal code.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.