Rozdział 16. Zend_File

Spis treści

16.1. Zend_File_Transfer
16.1.1. Supported adapters for Zend_File_Transfer
16.1.2. Options for Zend_File_Transfer
16.2. Validators for Zend_File_Transfer
16.2.1. Using validators with Zend_File_Transfer
16.2.2. Count validator
16.2.3. ExcludeExtension validator
16.2.4. ExcludeMimeType validator
16.2.5. Exists validator
16.2.6. Extension validator
16.2.7. FilesSize validator
16.2.8. ImageSize validator
16.2.9. IsCompressed validator
16.2.10. IsImage validator
16.2.11. MimeType validator
16.2.12. NotExists validator
16.2.13. Size validator

16.1. Zend_File_Transfer

Zend_File_Transfer enables developers to take control over file uploads and also over file downloads. It allows you to use built in validators for file purposes and gives you the ability even to change files with filters. Zend_File_Transfer works with adapters which allow to use the same API for different transport protocols like HTTP, FTP, WEBDAV and more.

[Notatka] Limitation

The current implementation of Zend_File_Transfer shipped in 1.6.0 is limited to HTTP Post Uploads. Download of files and other Adapters will be added in the next releases. Not implemented methods will throw an exception. So actually you should use an instance of Zend_File_Transfer_Adapter_Http directly. This will change in future, as soon as there are multiple adapters available.

The usage of Zend_File_Transfer is quite simple. It consist of two parts. The HTTP Form which does the upload, and the handling of the uploaded files with Zend_File_Transfer. See the following example:

Przykład 16.1. Simple File-Upload Form

This example illustrates a basic file upload which uses Zend_File_Transfer. The first part is the file form. In our example there is one file which we want 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>

Note that you should use Zend_Form_Element_File for your convenience instead of creating the HTML manually.

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

$adapter = new Zend_File_Transfer_Adapter_Http();

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

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

        

As you see the simplest usage is to define a destination with the setDestination method and to call the receive() method. If there are any upload errors then you will get them within an exception returned.


[Notatka] Attention

Keep in mind that this is just the simplest usage. You should never just use this example as is in an living environment as it causes severe security issues. You should always use validators to increase security.

16.1.1. Supported adapters for Zend_File_Transfer

Zend_File_Transfer is build to support different adapters and also directions. It is designed to allow uploading, downloading and even forwarding (upload one adapter and download with another adapter at the same time) of files. But with Zend Framework 1.6 there is only one adapter available, the Http adapter.

Because there is only one adapter available at this time, the base class is not ready for use. So if you want to use Zend_File_Transfer you will have to use the adapter directly.

16.1.2. Options for Zend_File_Transfer

Zend_File_Transfer and their adapters support different options. You can set all options either by giving them in the constructor, or by usage of setOptions($options). getOptions() will return you the actually set options. Attached you will find a listing of all supported options.

  • ignoreNoFile: If this option is set to true, all validators will ignore if the file has not been uploaded by the form. This option defaults to false which throws an error of the file was not given.