How to Easily Validate Archive File Formats in PHP

Cloudmersive
3 min readMay 20, 2024

Archive formats (like Zip, RAR, and TAR) can cause all kinds of downstream problems if we don’t properly validate them in our PHP applications.

Thankfully, by performing in-depth archive validation for common archive types, we can ensure compressed archives are error- and warning-free before we move those files to sensitive downstream locations in our application workflow.

In this case, we’ll quickly and easily handle our archive validation checks using a few free APIs.

We can structure each API call using the ready-to-run PHP code examples provided further down the page, and we can authorize all our API calls with a single free Cloudmersive API key (this will allow a limit of 800 API calls per month with zero commitments). Furthermore, we can execute a single Composer command to install the PHP client.

We’ll use the below code examples to validate Zip, GZip, RAR, and TAR archives.

First, let’s execute the below command to install the PHP client:

composer require cloudmersive/cloudmersive_document_convert_api_client

With SDK installation out of the way, let’s copy the below code to call our Zip archive validation API. When we call this API (or any of the other APIs featured below), we can replace the ‘YOUR_API_KEY’ placeholder text in the $config snippet with our own free API key:

<?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\ValidateDocumentApi(


new GuzzleHttp\Client(),
$config
);
$input_file = "/path/to/inputfile"; // \SplFileObject | Input file to perform the operation on.

try {
$result = $apiInstance->validateDocumentZipValidation($input_file);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling ValidateDocumentApi->validateDocumentZipValidation: ', $e->getMessage(), PHP_EOL;
}
?>

And let’s copy the below code to call the GZip archive validation API:

<?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\ValidateDocumentApi(


new GuzzleHttp\Client(),
$config
);
$input_file = "/path/to/inputfile"; // \SplFileObject | Input file to perform the operation on.

try {
$result = $apiInstance->validateDocumentGZipValidation($input_file);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling ValidateDocumentApi->validateDocumentGZipValidation: ', $e->getMessage(), PHP_EOL;
}
?>

We can copy the below code to call our RAR archive validation API:

<?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\ValidateDocumentApi(


new GuzzleHttp\Client(),
$config
);
$input_file = "/path/to/inputfile"; // \SplFileObject | Input file to perform the operation on.

try {
$result = $apiInstance->validateDocumentRarValidation($input_file);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling ValidateDocumentApi->validateDocumentRarValidation: ', $e->getMessage(), PHP_EOL;
}
?>

And, finally, we can use the following code to call the TAR validation api:

<?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\ValidateDocumentApi(


new GuzzleHttp\Client(),
$config
);
$input_file = "/path/to/inputfile"; // \SplFileObject | Input file to perform the operation on.

try {
$result = $apiInstance->validateDocumentTarValidation($input_file);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling ValidateDocumentApi->validateDocumentTarValidation: ', $e->getMessage(), PHP_EOL;
}
?>

Each API response will contain information structured like the JSON example below:

{
"DocumentIsValid": true,
"PasswordProtected": true,
"ErrorCount": 0,
"WarningCount": 0,
"ErrorsAndWarnings": [
{
"Description": "string",
"Path": "string",
"Uri": "string",
"IsError": true
}
]
}

That’s all there is to it — now we can easily validate different archive formats using just a few lines of PHP code.

--

--

Cloudmersive

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