Chapter 19. Zend_File

Table of Contents

19.1. Zend_File_Transfer
19.1.1. Supported Adapters for Zend_File_Transfer
19.1.2. Options for Zend_File_Transfer
19.1.3. Checking Files
19.1.4. Additional File Informations
19.2. Validators for Zend_File_Transfer
19.2.1. Using Validators with Zend_File_Transfer
19.2.2. Count Validator
19.2.3. Crc32 Validator
19.2.4. ExcludeExtension Validator
19.2.5. ExcludeMimeType Validator
19.2.6. Exists Validator
19.2.7. Extension Validator
19.2.8. FilesSize Validator
19.2.9. ImageSize Validator
19.2.10. IsCompressed Validator
19.2.11. IsImage Validator
19.2.12. Hash Validator
19.2.13. Md5 Validator
19.2.14. MimeType Validator
19.2.15. NotExists Validator
19.2.16. Sha1 Validator
19.2.17. Size Validator
19.2.18. WordCount Validator
19.3. Filters for Zend_File_Transfer
19.3.1. Using filters with Zend_File_Transfer
19.3.2. Decrypt filter
19.3.3. Encrypt filter
19.3.4. LowerCase filter
19.3.5. Rename filter
19.3.6. UpperCase filter
19.4. Migrating from previous versions
19.4.1. Migrating from 1.6 to 1.7 or newer
19.4.1.1. Changes when using filters and validators
19.4.1.1.1. Filter: Rename
19.4.1.1.2. Validator: Count
19.4.1.1.3. Validator: Extension
19.4.1.1.4. Validator: FilesSize
19.4.1.1.5. Validator: Hash
19.4.1.1.6. Validator: ImageSize
19.4.1.1.7. Validator: Size
19.4.2. Migrating from 1.6.1 to 1.6.2 or newer
19.4.2.1. Changes when using validators

19.1. Zend_File_Transfer

Zend_File_Transfer provides extensive support for file uploads and downloads. It comes with built-in validators for files plus functionality to change files with filters. Protocal adapters allow Zend_File_Transfer to expose the same API for transport protocols like HTTP, FTP, WEBDAV and more.

[Note] Limitation

The current implementation of Zend_File_Transfer shipped in 1.6.0 is limited to HTTP Post Uploads. Other adapters supporting downloads and other protocols will be added in future releases. Unimplemented methods will throw an exception. For now, you should use Zend_File_Transfer_Adapter_Http directly. As soon as there are multiple adapters available you can use a common interface.

[Note] Forms

When you are using Zend_Form you should use the APIs provided by Zend_Form and not Zend_File_Transfer directly. The file transfer support in Zend_Form is implemented with Zend_File_Transfer, so the information in this chapter may be useful for advanced users of Zend_Form.

The usage of Zend_File_Transfer is relatively simple. It consists of two parts. The HTTP form does the upload, while the Zend_File_Transfer handles the uploaded files. See the following example:

Example 19.1. Simple Form for Uploading Files

This example illustrates basic file uploading. The first part is the file form. In our example there is one file to upload.

<form enctype="multipart/form-data" action="/file/upload" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
        Choose a file to upload: <input name="uploadedfile" type="file" />
    <br />
    <input type="submit" value="Upload File" />
</form>

For convenience, you can use Zend_Form_Element_File instead of building the HTML manually.

The next step is to create the receiver of the upload. In our example the receiver is located at /file/upload. So next we will create the file controller and the upload action.

$adapter = new Zend_File_Transfer_Adapter_Http();

$adapter->setDestination('C:\temp');

if (!$adapter->receive()) {
    $messages = $adapter->getMessages();
    echo implode("\n", $messages);
}

        

This code listing demonstrates the simplest usage of Zend_File_Transfer. A local destination is set with the setDestination method, then the receive() method is called. If there are any upload errors, an error will be returned.


[Note] Attention

This example is suitable only for demonstrating the basic API of Zend_File_Transfer. You should never use this code listing in a production environment, because severe security issues may be introduced. You should always use validators to increase security.

19.1.1. Supported Adapters for Zend_File_Transfer

Zend_File_Transfer is designed to support a variety of adapters and transfer directions. With Zend_File_Transfer you can upload, download and even forward (upload one adapter and download with another adapter at the same time) files.

19.1.2. Options for Zend_File_Transfer

Zend_File_Transfer and its adapters support different options. You can set all options either by passing them to the constructor or by calling setOptions($options). getOptions() will return the options that are currently set. The following is a list of all supported options.

  • ignoreNoFile: If this option is set to true, all validators will ignore files that have not been uploaded by the form. The default value is false which results in an error if no files were specified.

19.1.3. Checking Files

Zend_File_Transfer has several methods that check for various states of the specified file. These are useful if you must process files after they have been uploaded. These methods include:

  • isValid($files = null): This method will check if the given files are valid, based on the validators that are attached to the files. If no files are specified, all files will be checked. You can call isValid() before calling receive(); in this case, receive() will not call isValid internally again when receiving the file.

  • isUploaded($files = null): This method will check if the specified files have been uploaded by the user. This is useful when you have defined one or more optional files. When no files are specified, all files will be checked.

  • isReceived($files = null): This method will check if the given files have already been received. When no files are specified, all files will be checked.

Example 19.2. Checking Files

$upload = new Zend_File_Transfer();

// Returns all known internal file information
$files = $upload->getFileInfo();

foreach ($files as $file => $info) {
    // file uploaded ?
    if (!$upload->isUploaded($file)) {
        print "Why havn't you uploaded the file ?";
        continue;
    }

    // validators are ok ?
    if (!$upload->isValid($file)) {
        print "Sorry but $file is not what we wanted";
        continue;
    }
}

$upload->receive();

            

19.1.4. Additional File Informations

Zend_File_Transfer can return additional information on files. The following methods are available:

  • getFileName($file = null, $path = true): This method will return the real file name of a transferred file.

  • getFileInfo($file = null): This method will return all internal information for the given file.

  • getFileSize($file = null): This method will return the real filesize for the given file.

  • getHash($hash = 'crc32', $files = null): This method returns a hash of the content of a given transferred file.

getFileName() accepts the name of the element as first parameter. If no name is given, all known filenames will be returned in an array. If the file is a multifile, you will also get an array. If there is only a single file a string will be returned.

By default file names will be returned with the complete path. If you only need the file name without path, you can set the second parameter, $path, which will truncate the file path when set to false.

Example 19.3. Getting the Filename

$upload = new Zend_File_Transfer();
$upload->receive();

// Returns the file names from all files
$names = $upload->getFileName();

// Returns the file names from the 'foo' form element
$names = $upload->getFileName('foo');

            

[Note] Note

Note that the file name can change after you receive the file, because all filters will be applied once the file is received. So you should always call getFileName() after the files have been recieved.

getFileSize() returns per default the real filesize in SI notation which means you will get 2kB instead of 2048. If you need only the plain size set the useByteString option to false.

Example 19.4. Getting the size of a file

$upload = new Zend_File_Transfer();
$upload->receive();

// Returns the sizes from all files as array if more than one file was uploaded
$size = $upload->getFileSize();

// Switches of the SI notation to return plain numbers
$upload->setOption(array('useByteString' => false));
$size = $upload->getFileSize();

            

getHash() accepts the name of a hash algorithm as first parameter. For a list of known algorithms refer to PHP's hash_algos method. If you don't specify an algorithm, the crc32 algorithm will be used by default.

Example 19.5. Getting the hash of a file

$upload = new Zend_File_Transfer();
$upload->receive();

// Returns the hashes from all files as array if more than one file was uploaded
$hash = $upload->getHash('md5');

// Returns the hash for the 'foo' form element
$names = $upload->getHash('crc32', 'foo');

            

[Note] Note

Note that if the given file or form name contains more than one file, the returned value will be an array.