Spis treści
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.
![]() |
Limitation |
|---|---|
The current implementation of |
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.
![]() |
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. |
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.
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.