Welcome to 3DMICK
info@3dmick.com
+91 (971) 119-1127

ZF Transfer Control

3h

ZF Transfer Control

One of the powerful components of Zend Framework is the Zend_File_Transfer component which is useful while uploading and downloading files from a user’s computer to a Web server. This component is more flexible than handling uploads; it can also be used to transfer files using protocols such as FTP. 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. In website development a common activity that we need to perform is to transfer controls from one page to another.

The current implementation of Zend_File_Transfer is limited to HTTP Post Uploads. Use Zend_File_Transfer_Adapter_Http directly. As soon as there are multiple adapters available you can use a common interface.

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.

  • This example is for 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: <inputname="uploadedfile" type="file" />
</>
<input type="submit" value="Upload File" />
</form>

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

$test = new Zend_File_Transfer_Adapter_Http();
$test->setDestination('C:\temp');
if (!$test->receive()) {
$messages = $test->getMessages();
echo implode("\n", $messages);
}

This code 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.

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.

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.