PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Using filters with Zend_File_Transfer

The usage of filters is quite simple. There are several methods for adding and manipulating filters.

  • addFilter($filter, $options = null, $files = null): Adds the given filter to the filter stack (optionally only to the file(s) specified). $filter may be either an actual filter instance, or a short name specifying the filter type (e.g., 'Rename').

  • addFilters(array $filters, $files = null): Adds the given filters to the stack of filters. Each entry may be either a filter type/options pair, or an array with the key 'filter' specifying the filter (all other options will be considered filter options for instantiation).

  • setFilters(array $filters, $files = null): Overwrites any existing filters with the filters specified. The filters should follow the syntax for addFilters().

  • hasFilter($name): Indicates if a filter has been registered.

  • getFilter($name): Returns a previously registered filter.

  • getFilters($files = null): Returns registered filters; if $files is passed, returns filters for that particular file or set of files.

  • removeFilter($name): Removes a previously registered filter.

  • clearFilters(): Clears all registered filters.

Example 428. Add filters to a file transfer

<?php
$upload 
= new Zend_File_Transfer();

// Set a new destination path
$upload->addFilter('Rename''C:\picture\uploads');

// Set a new destination path and overwrites existing files
$upload->addFilter('Rename',
                   array(
'target' => 'C:\picture\uploads',
                         
'overwrite' => true));

Example 429. Limit filters to single files

addFilter(), addFilters(), and setFilters() 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 filter.

<?php
$upload 
= new Zend_File_Transfer();

// Set a new destination path and limits it only to 'file2'
$upload->addFilter('Rename''C:\picture\uploads''file2');

Generally you should simply use the addFilters() method, which can be called multiple times.

Example 430. Add multiple filters

Often it's simpler just to call addFilter() multiple times. One call for each filter. 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:

<?php
$upload 
= new Zend_File_Transfer();

// Set a filesize with 20000 bytes
$upload->addFilter('Rename''C:\picture\newjpg''file1')
       ->
addFilter('Rename''C:\picture\newgif''file2');

Note

Note that even though setting the same filter multiple times is allowed, doing so can lead to issues when using different options for the same filter.

Zend Framework