ZIP file validator

Enables runtime validation of ZIP files with error reporting.

When installed, this module automatically validates any uploaded ZIP files. It is also used by the $files->unzip() method to validate ZIP files before extracting them. This module includes the following types of validations:

  • Maximum file limit (maxFiles)
  • Minimum file requirement (minFiles)
  • Maximum directory/folder depth (maxDepth)
  • Maximum total uncompressed size of all files in ZIP (maxTotalMegabytes)
  • Maximum size of any individual file in ZIP (maxFileMegabytes)
  • Maximum allowed compression ratio (maxCompRatio)
  • Requiring that certain files are present for ZIP to be valid (requireFiles)
  • Filename patterns that cause entire ZIP to be invalid (fatalFiles)
  • Whether or not file encryption is allowed (allowEncrypted)

The requireFiles and fatalFiles optionally support regex patterns. When using regex patterns, the ! character must be used as the starting/ending delimiter.

// Usage example
$v = $modules->get('FileValidatorZip');
$v->setVerbose(true); // return verbose errors (useful for debugging)

// Below are all settings with default values. You do not have to specify the
// settings unless you want to change them from the defaults.

$v->minFiles = 1; // Min files allowed
$v->maxFiles = 1000; // Max files allowed
$v->maxDepth = 8; // Max directory/folder depth allowed
$v->maxFileMegabytes = 20; // Max uncompressed megabytes for any 1 file in ZIP
$v->maxTotalMegabytes = 100; // Max uncompressed total megabytes for entire ZIP
$v->maxErrors = 10; // Max errors to record before stopping
$v->maxCompRatio = 0; // Max compression ratio (0=disabled)
$v->allowEncrypted = false; // Allow encrypted files in ZIP?
$v->requireFiles = []; // filenames or regex patterns that must match in at least one file for ZIP to be valid
$v->fatalFiles = []; // filenames or regex patterns that if matched in any file cause ZIP to be invalid

// validating a ZIP file
if($v->isValid('/path/to/file.zip')) {
  echo "<h3>ZIP file is valid!</h3>";
} else {
  echo "<h3>ZIP file is invalid!</h3><ul>";
  echo $v->errors()->render();
}

Click any linked item for full usage details and examples. Hookable methods are indicated with the icon. In addition to those shown below, the FileValidatorZip class also inherits all the methods and properties of: FileValidatorModule, WireData and Wire.

Show class?     Show args?       Only hookable?    

Properties

NameReturnSummary 
FileValidatorZip::allowEncrypted bool Allow encrypted files?
DEFAULT: false
 
FileValidatorZip::fatalFiles array File names or regex patterns that when matched, cause entire validation to fail
DEFAULT: []
 
FileValidatorZip::maxCompRatio int Max allowed compression ratio or 0 to ignore
DEFAULT: 0
 
FileValidatorZip::maxDepth int Maximum allowed folder/directory depth in ZIP
DEFAULT: 8
 
FileValidatorZip::maxErrors int Maximum number of errors to report before giving up
DEFAULT: 10
 
FileValidatorZip::maxFileMegabytes int Maximum allowed uncompressed size of any individual file in ZIP in MB.
DEFAULT: 20
 
FileValidatorZip::maxFiles int Maximum number of files allowed in ZIP
DEFAULT: 1000
 
FileValidatorZip::maxTotalMegabytes int Maximum allowed total uncompressed size of all files in ZIP in MB.
DEFAULT: 50
 
FileValidatorZip::minFiles int Minimum number files that must be present for a valid ZIP
DEFAULT: 1
 
FileValidatorZip::requireFiles array File names or regex patterns that must be present for ZIP to be valid.
DEFAULT: []
 

Additional methods and properties

In addition to the methods and properties above, FileValidatorZip also inherits the methods and properties of these classes:

API reference based on ProcessWire core version 3.0.253