FileValidatorZip
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 File class also inherits all the methods and properties of: FileValidatorModule, WireData and Wire.
Common
| Name | Return | Summary | |
|---|---|---|---|
File File File | None | ||
File File File | string | Get reason why last isValidFilename() returned false | |
File File File | bool | Is the given filename allowed for extraction from a ZIP file? | |
File File File | None | Set verbose mode for error reporting (useful for debugging) | |
File File File | None | If a ZipArchive instance is already available it can be provided here |
Properties
| Name | Return | Summary | |
|---|---|---|---|
| File | bool | Allow encrypted files? DEFAULT: false | |
| File | array | File names or regex patterns that when matched, cause entire validation to fail DEFAULT: [] | |
| File | int | Max allowed compression ratio or 0 to ignore DEFAULT: 0 | |
| File | int | Maximum allowed folder/directory depth in ZIP DEFAULT: 8 | |
| File | int | Maximum number of errors to report before giving up DEFAULT: 10 | |
| File | int | Maximum allowed uncompressed size of any individual file in ZIP in MB. DEFAULT: 20 | |
| File | int | Maximum number of files allowed in ZIP DEFAULT: 1000 | |
| File | int | Maximum allowed total uncompressed size of all files in ZIP in MB. DEFAULT: 50 | |
| File | int | Minimum number files that must be present for a valid ZIP DEFAULT: 1 | |
| File | 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, File
API reference based on ProcessWire core version 3.0.263