How to virus scan a ZIP archive in PHP
1 min readMar 4, 2020
A zip file can hide a hornet’s nest of dangerous malware and other security threats. To guard against this, we have two options. We can spend hours coding, or we can implement virus scanning functionality in a few minutes using an API. Today we are going to be using the latter method.
Starting things off, we shall install our client via the command line of Composer.
composer require cloudmersive/cloudmersive_virusscan_api_client
Once that’s done, activate scanFile, as you see here.
<?phprequire_once(__DIR__ . '/vendor/autoload.php');// Configure API key authorization: Apikey$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed// $config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKeyPrefix('Apikey', 'Bearer');$apiInstance = new Swagger\Client\Api\ScanApi(// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.// This is optional, `GuzzleHttp\Client` will be used as default.new GuzzleHttp\Client(),$config);$input_file = "/path/to/file"; // \SplFileObject | Input file to perform the operation on.try {$result = $apiInstance->scanFile($input_file);print_r($result);} catch (Exception $e) {echo 'Exception when calling ScanApi->scanFile: ', $e->getMessage(), PHP_EOL;}?>
And it’s just that easy. The API will kick back our results after performing the requesting scan.