|
|
Zend_File_Transfer is delivered with several file related validators which should be
used to increase security and prevent possible attacks. Note that the validators are only as good
as you are using them. All validators which are provided with Zend_File_Transfer can
be found in the Zend_Validator component and are named Zend_Validate_File_*.
The following validators are actually available:
Count: This validator checks for the ammount of files. It provides a
minimum and a maximum and will throw an error when any of these are crossed.
Extension: This validator checks the extension of files. It will throw an
error when an given file has an undefined extension.
FilesSize: This validator checks the complete size of all validated files.
It remembers internally the size of all checked files and throws an error when the sum of
all files exceed the defined size. It does also provide a minimum and a maximum size.
Size: This validator is able to check files for it's filesize. It
provides a minimum and a maximum size and will throw an error when any of these are crossed.
Upload: This validator is an internal one, which checks if a upload has
produced a problem. You must not set it, as it's automatically set by
Zend_File_Transfer itself. So you can forget this validator. You should only
know that it exists.
Zend_File_TransferThe usage of validators is quite simple. There are several methods for adding and manipulating validators.
addValidator($validator, $options = null, $files =
null): Adds the given validator to the validator
stack (optionally only to the file(s) specified).
$validator may be either an actual validator
instance, or a short name specifying the validator type
(e.g., 'Count').
addValidators(array $validators, $files =
null): Adds the given validators to the stack of
validators. Each entry may be either a validator
type/options pair, or an array with the key 'validator'
specifying the validator (all other options will be
considered validator options for instantiation).
setValidators(array $validators, $files =
null): Overwrites any existing validators with
the validators specified. The validators should follow the
syntax for addValidators().
hasValidator($name): Indicates if a
validator has been registered.
getValidator($name): Returns a previously
registered validator.
getValidators($files = null): Returns
registered validators; if $files is passed,
returns validators for that particular file or set of
files.
removeValidator($name): Removes a previously
registered validator.
clearValidators(): Clears all
registered validators.
Example #1 Add validators to a file transfer
$upload = new Zend_File_Transfer();
// Set a filesize with 20000 bytes
$upload->addValidator('Size', 20000);
// Set a filesize with 20 bytes minimum and 20000 bytes maximum
$upload->addValidator('Size', array(20, 20000));
// Set a filesize with 20 bytes minimum and 20000 bytes maximum and a file count in one step
$upload->setValidators(array(
'Size' => array(20, 20000),
'Count' => array(1, 3),
));
Example #2 Limit validators to single files
addValidator(), addValidators(), and
setValidators() each accept a final
$files argument. This argument can be used to
specify a particular file or array of files on which to set the
given validator.
$upload = new Zend_File_Transfer();
// Set a filesize with 20000 bytes and limits it only to 'file2'
$upload->addValidator('Size', 20000, 'file2');
Generally you should simply use the addValidators() method, which can be called
multiple times.
Example #3 Add multiple validators
Often it's simpler just to call addValidator() multiple times. One call for each
validator. This also increases the readability and makes your code more maintainable. As all
methods provide a fluent interface you can couple the calls as shown below:
$upload = new Zend_File_Transfer();
// Set a filesize with 20000 bytes
$upload->addValidator('Size', 20000)
->addValidator('Count', 2)
->addValidator('Filessize', 25000);
Note:
Note that even though setting the same validator multiple times is allowed, doing so can lead to issues when using different options for the same validator.
The Count validator checks for the number of files which are provided. It
supports the following options:
Min: Sets the minimum number of files to transfer.
Note:
Beware: When using this option you must give the minimum number of files when calling this validator the first time; otherwise you will get an error in return.
With this option you can define the minimum number of files you expect to receive.
Max: Set the maximum number of files to transfer.
With this option you can limit the number of files which are accepted but also detect a possible attack when more files are given than defined in your form.
You can initiate this validator with both options. The first option is min,
the second option is max. When only one option is given it is used as max.
But you can also use the methods setMin() and setMax() to set both
options afterwards and getMin() and getMax() to retrieve the actual
set values.
Example #4 Using the Count validator
$upload = new Zend_File_Transfer();
// Limit the amount of files to maximum 2
$upload->addValidator('Count', 2);
// Limit the amount of files to maximum 5 and expects minimum 1 file to be returned
$upload->addValidator('Count', array(1, 5);
Note:
Note that this validator stores the number of checked files internally. The file which exceeds the maximum will be returned as error.
The Extension validator checks the file extension of files which are provided. It
supports the following options:
Extension: Checks if the given file uses this file extension.
This validator accepts multiple extensions either as a comma-delimited string, or as an
array. You may also use the methods setExtension(), addExtension(),
and getExtension() to set and retrieve extensions.
Example #5 Using the Extension validator
$upload = new Zend_File_Transfer();
// Limit the extensions to jpg and png files
$upload->addValidator('Extension', 'jpg,png');
// Limit the extensions to jpg and png files but use array notation
$upload->addValidator('Extension', array('jpg', 'png'));
Note:
Note that this validator just checks the file extension. It does not check the actual file MIME type.
The FilesSize validator checks for the aggregate size of all transferred files.
It supports the following options:
Min: Sets the minimum aggregate filesize.
With this option you can define the minimum aggregate filesize of files you expect to transfer.
Max: Sets the maximum aggregate filesize.
With this option you can limit the aggregate filesize of all files which are transferred, but not the filesize of individual files.
You can initiate this validator with both options. The first option is min,
the second option is max. When only one option is given it is used as max.
But you can also use the methods setMin() and setMax() to set both
options afterwards and getMin() and getMax() to receive the actual
set values.
The size itself is also accepted in SI notation as done by most operating systems. Instead of
20000 bytes you can just give 20kB. All units are converted
by using 1024 as base value. The following Units are accepted: kB, MB,
GB, TB, PB and EB. As mentioned you have to
note that 1kB is equal to 1024 bytes.
Example #6 Using the FilesSize validator
$upload = new Zend_File_Transfer();
// Limit the size of all given files to 40000 bytes
$upload->addValidator('FilesSize', 40000);
// Limit the size of all given files to maximum 4MB and mimimum 10kB
$upload->setValidator('FilesSize', array('10kB', '4MB');
Note:
Note that this validator stores the filesize of checked files internally. The file which exceeds the size will be returned as error.
The Size validator checks for the size of a single file. It
supports the following options:
Min: Set the minimum filesize.
With this option you can define the minimum filesize for an individual file you expect to transfer.
Max: Set the maximum filesize.
With this option you can limit the filesize of a single file you tranfer.
You can initiate this validator with both options. The first option is min,
the second option is max. When only one option is given it is used as max.
But you can also use the methods setMin() and setMax() to set both
options afterwards and getMin() and getMax() to receive the actual
set values.
The size itself is also accepted in SI notation as done by most operating systems. Instead of
20000 bytes you can just give 20kB. All units are conterted
by using 1024 as base value. The following Units are accepted: kB, MB,
GB, TB, PB and EB. As mentioned you have to
note that 1kB is equal to 1024 bytes.
Example #7 Using the Size validator
$upload = new Zend_File_Transfer();
// Limit the size of a file to 40000 bytes
$upload->addValidator('Size', 40000);
// Limit the size a given file to maximum 4MB and mimimum 10kB and limits this
// validator to the file "uploadfile"
$upload->addValidator('Size', array('10kB', '4MB', 'uploadfile');
|
|
Copyright © 2005-2011 Zend Technologies Inc (compiled by mikaelkael with ZFDocumentor - SVN 12849).

