A free and open-source book on ZF3 for beginners

10.5. Filtering Uploaded Files

Zend Framework 3 provides several filters intended for "transforming" file fields. Those filter classes (listed in table 10.2) belong to Zend\Filter component and live in Zend\Filter\File namespace.

Table 10.2. Standard File Filters
Class name Short alias Description
Rename FileRename Renames/moves an arbitrary file.
RenameUpload FileRenameUpload Renames/moves the uploaded file with security checks.
Encrypt FileEncrypt Encrypts a given file and stores the encrypted file content.
Decrypt FileDecrypt Decrypts a given file and stores the decrypted file content.
LowerCase FileLowerCase Converts file content to lower case letters.
UpperCase FileUpperCase Converts file content to upper case letters.

From the table, you can see that filters can be divided into the following groups:

Please note that since file filters live in Zend\Filter\File namespace, their short aliases (that you use when creating a filter with the factory) start with File prefix. For example, the RenameUpload filter has FileRenameUpload alias.

The Encrypt and Decrypt filters allow to apply various encryption/decryption algorithms to the uploaded file (concrete algorithm is attached by specifying the certain adapter). The LowerCase and UpperCase filters are suitable for converting text files to lower- and upper-case, respectively 43.

43) In the author's opinion, the above mentioned four filters are not very useful when working with uploaded files, because you rarely need to encrypt an uploaded file or convert it to lower case letters.

The Rename filter allows to rename and/or move an arbitrary file (not only uploaded file). It uses the rename() PHP function internally, and that's why it is in general not recommended to use this filter with uploaded files because of security reasons.

The RenameUpload filter seems to be much more useful than other filters, because it allows to encapsulate the call of the move_uploaded_file() function and move/rename the uploaded file from a temporary location to its persistent directory. We will show how to use the RenameUpload filter in the Image Gallery code example later in this chapter.