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

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. Protocol adapters allow Zend_File_Transfer to expose the same API for transport protocols like HTTP, FTP, WEBDAV and more.

Limitation

The current implementation of Zend_File_Transfer 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.

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

<?php
$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.


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.

Zend Framework