How to Validate RAR Riles in PHP
Writing code to validate archives like RAR can be a challenge. Thankfully, we can easily validate RAR files using complementary PHP code examples.
We’ll perform our RAR file validation using a free API, and in our API response, we’ll get detailed information about any errors and warnings found in invalid RAR files.
To begin structuring our API call, let’s first install the client SDK. We can execute the below command from our command line to install using Composer:
composer require cloudmersive/cloudmersive_document_convert_api_client
Next, let’s grab a free Cloudmersive API key to authorize our API calls. We can make up to 800 API calls per month with a free API key (once we reach our limit, our total will simply reset the following month).
Finally, let’s copy the below code examples to call the function. We can load our RAR file path in the $input_file variable:
<?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;
}
?>
All done — no more code required. We now have a simple, low-code PHP solution for validating RAR archive files.