Programmer's Reference Guide

Zend_File_Transfer

Validators for Zend_File_Transfer

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 amount of files. It provides a minimum and a maximum and will throw an error when any of these are crossed.

  • Exists: This validator checks for the existence of files. It will throw an error when an given file does not exist.

  • 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.

  • ImageSize: This validator checks the size of image. It validates the width and height and provides for both a minimum and a maximum size.

  • MimeType: This validator can validate the mimetype of files. It is also able to validate types of mimetypes and will throw error when the mimetype of a given file does not match.

  • NotExists: This validator checks for the existence of files. It will throw an error when an given file does exist.

  • 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.

Using validators with Zend_File_Transfer

The 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.

Count 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.

Exists validator

The Exists validator checks for the existence of files which are provided. It supports the following options:

  • directory: Checks if the file exists in the given directory.

This validator accepts multiple directories either as a comma-delimited string, or as an array. You may also use the methods setDirectory(), addDirectory(), and getDirectory() to set and retrieve directories.

Example #5 Using the Exists validator

$upload = new Zend_File_Transfer();

// Add the temp directory to check for
$upload->addValidator('Exists', '\temp');

// Add two directories using the array notation
$upload->addValidator('Exists', array('\home\images', '\home\uploads'));

            

Note: Note that this validator checks if the file exists in all set directories. The validation will fail if the file does not exist in any of the given directories.

Extension validator

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.

  • case: Sets if validation should be done case sensitive. Default is not case sensitive. Note the this option is used for all used extensions.

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.

In some cases it is usefull to test case sensitive. Therefor the constructor allows a second parameter $case which, if set to true, will validate the extension case sensitive.

Example #6 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'));

// Check case sensitive
$upload = new Zend_File_Transfer('mo,png', true);
if (!$upload->isValid('C:\temp\myfile.MO')) {
    print 'Not valid due to MO instead of mo';
}

            

Note: Note that this validator just checks the file extension. It does not check the actual file MIME type.

FilesSize validator

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 #7 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.

ImageSize validator

The ImageSize validator checks for the size of image files. It supports the following options:

  • minheight: Sets the minimum image height.

    With this option you can define the minimum height of the image you want to validate.

  • maxheight: Sets the maximum image height.

    With this option you can limit the maximum height of the image you want to validate.

  • minwidth: Sets the minimum image width.

    With this option you can define the minimum width of the image you want to validate.

  • maxwidth: Sets the maximum image width.

    With this option you can limit the maximum width of the image you want to validate.

You can initiate this validator with all four options set. When minheight or minwidth are not given, they will be set to 0. And when maxwidth or maxheight are not given, they will be set to null. But you can also use the methods setImageMin() and setImageMax() to set both minimum and maximum values to set the options afterwards and getMin() and getMax() to receive the actual set values.

For your convinience there is also a setImageWidth and setImageHeight method which will set the minimum and maximum height and width. Of course also the related getImageWidth and getImageHeight methods are available.

To suppress the validation of a dimension just set the related value to null.

Example #8 Using the ImageSize validator

$upload = new Zend_File_Transfer();

// Limit the size of a image to a height of 100-200 and a width of
// 40-80 pixel
$upload->addValidator('ImageSize', 40, 100, 80, 200);

// Use the array notation
$upload->setValidator('ImageSize', array(40, 100, 80, 200);

// Use the named array notation
$upload->setValidator('ImageSize', 
                      array('minwidth' => 40, 
                            'maxwidth' => 80, 
                            'minheight' => 100, 
                            'maxheight' => 200)
                      );

// Set other image dimensions
$upload->setImageWidth(20, 200);

            

MimeType validator

The MimeType validator checks for the mimetype of transferred files. It supports the following options:

  • MimeType: Set the mimetype type to validate against.

    With this option you can define the mimetype of files which will be accepted.

This validator accepts multiple mimetype either as a comma-delimited string, or as an array. You may also use the methods setMimeType(), addMimeType(), and getMimeType() to set and retrieve mimetype.

Example #9 Using the MimeType validator

$upload = new Zend_File_Transfer();

// Limit the mimetype of all given files to gif images
$upload->addValidator('MimeType', 'image/gif');

// Limit the mimetype of all given files to gif and jpeg images
$upload->setValidator('MimeType', array('image/gif', 'image/jpeg');

// Limit the mimetype of all given files to the group images
$upload->setValidator('MimeType', 'image');

            

The above example shows that it is also possible to limit the accepted mimetype to a group of mimetypes. To allow all images just use 'image' as mimetype. This can be used for all groups of mimetypes like 'image', 'audio', 'video', 'text, and so on.

Note: Note that allowing groups of mimetypes will accept all members of this group even if your application does not support them. When you allow 'image' you will also get 'image/xpixmap' or 'image/vasa' which could be problematic. When you are not sure if your application supports all types you should better allow only defined mimetypes instead of the complete group.

NotExists validator

The NotExists validator checks for the existence of files which are provided. It supports the following options:

  • directory: Checks if the file does not exist in the given directory.

This validator accepts multiple directories either as a comma-delimited string, or as an array. You may also use the methods setDirectory(), addDirectory(), and getDirectory() to set and retrieve directories.

Example #10 Using the NotExists validator

$upload = new Zend_File_Transfer();

// Add the temp directory to check for
$upload->addValidator('NotExists', '\temp');

// Add two directories using the array notation
$upload->addValidator('NotExists', 
                      array('\home\images',
                            '\home\uploads')
                     );

            

Note: Note that this validator checks if the file does not exist in all of the set directories. The validation will fail if the file does exist in any of the given directories.

Size validator

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 transfer.

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 #11 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');

            

Zend_File_Transfer
blog comments powered by Disqus

Select a Version

Languages Available

Components

Search the Manual