Programmer's Reference Guide

Zend_File

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.

Note: 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:

Example #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.

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


Zend_File
blog comments powered by Disqus

Select a Version

Languages Available

Components

Search the Manual