Zend Framework comes with a set of classes for validating files, such as file size validation and CRC checking.
Note
All of the File validators’ filter() methods support both a file path string or a $_FILES array as the supplied argument. When a $_FILES array is passed in, the tmp_name is used for the file path.
Zend\Validator\File\Crc32 allows you to validate if a given file’s hashed contents matches the supplied crc32 hash(es). It is subclassed from the Hash validator to provide a convenient validator that only supports the crc32 algorithm.
Note
This validator requires the Hash extension from PHP with the crc32 algorithm.
Supported Options
The following set of options are supported:
Hash to test the file against.
Usage Examples
1 2 3 4 5 6 7 8 9 10 | // Does file have the given hash?
$validator = new \Zend\Validator\File\Crc32('3b3652f');
// Or, check file against multiple hashes
$validator = new \Zend\Validator\File\Crc32(array('3b3652f', 'e612b69'));
// Perform validation with file path
if ($validator->isValid('./myfile.txt')) {
// file is valid
}
|
Public Methods
Returns the current set of crc32 hashes.
Return type: | array |
---|
Adds a crc32 hash for one or multiple files to the internal set of hashes.
Parameters: | $options – See Supported Options section for more information. |
---|
Sets a crc32 hash for one or multiple files. Removes any previously set hashes.
Parameters: | $options – See Supported Options section for more information. |
---|
Zend\Validator\File\ExcludeExtension checks the extension of files. It will assert false when a given file has one the a defined extensions.
This validator is inversely related to the Extension validator.
Please refer to the Extension validator for options and usage examples.
Zend\Validator\File\ExcludeMimeType checks the MIME type of files. It will assert false when a given file has one the a defined MIME types.
This validator is inversely related to the MimeType validator.
Please refer to the MimeType validator for options and usage examples.
Zend\Validator\File\Exists checks for the existence of files in specified directories.
This validator is inversely related to the NotExists validator.
Supported Options
The following set of options are supported:
Comma-delimited string (or array) of directories.
Usage Examples
1 2 3 4 5 6 7 8 9 10 | // Only allow files that exist in ~both~ directories
$validator = new \Zend\Validator\File\Exists('/tmp,/var/tmp');
// ...or with array notation
$validator = new \Zend\Validator\File\Exists(array('/tmp', '/var/tmp'));
// Perform validation
if ($validator->isValid('/tmp/myfile.txt')) {
// file is valid
}
|
Note
This validator checks whether the specified file exists in all of the given directories. The validation will fail if the file does not exist in one (or more) of the given directories.
Zend\Validator\File\Extension checks the extension of files. It will assert true when a given file has one the a defined extensions.
This validator is inversely related to the ExcludeExtension validator.
Supported Options
The following set of options are supported:
Comma-delimited string (or array) of extensions to test against.