Zend_File_TransferZend_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.
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 #1 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.
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.
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.
Supported Adapters for Zend_File_TransferZend_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. Options for Zend_File_TransferZend_File_Transfer and its adapters support different options. You can set all options either by passing them to the constructor or by calling setOptions($options). getOptions() will return the options that are currently set. The following is a list of all supported options.
Checking FilesZend_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:
Example #2 Checking Files
Additional File InformationsZend_File_Transfer can return additional information on files. The following methods are available:
getFileName() accepts the name of the element as first parameter. If no name is given, all known filenames will be returned in an array. If the file is a multifile, you will also get an array. If there is only a single file a string will be returned. By default file names will be returned with the complete path. If you only need the file name without path, you can set the second parameter, $path, which will truncate the file path when set to FALSE. Example #3 Getting the Filename
getFileSize() returns per default the real filesize in SI notation which means you will get 2kB instead of 2048. If you need only the plain size set the useByteString option to FALSE. Example #4 Getting the size of a file
getHash() accepts the name of a hash algorithm as first parameter. For a list of known algorithms refer to » PHP's hash_algos method. If you don't specify an algorithm, the crc32 algorithm will be used by default. Example #5 Getting the hash of a file
getMimeType() returns the mimetype of a file. If more than one file was uploaded it returns an array, otherwise a string. Example #6 Getting the mimetype of a file
Warning
Possible exceptionNote that this method uses the fileinfo extension if it is available. If this extension can not be found, it uses the mimemagic extension. When no extension was found it raises an exception. Warning
Original data within $_FILESDue to security reasons also the original data within $_FILES will be overridden as soon as Zend_File_Transfer is initiated. When you want to omit this behaviour and have the original data simply set the detectInfos option to FALSE at initiation. This option will have no effect after you initiated Zend_File_Transfer. Progress for file uploadsZend_File_Transfer can give you the actual state of a fileupload in progress. To use this feature you need either the APC extension which is provided with most default PHP installations, or the UploadProgress extension. Both extensions are detected and used automatically. To be able to get the progress you need to meet some prerequisites. First, you need to have either APC or UploadProgress to be enabled. Note that you can disable this feature of APC within your php.ini. Second, you need to have the proper hidden fields added in the form which sends the files. When you use Zend_Form_Element_File this hidden fields are automatically added by Zend_Form. When the above two points are provided then you are able to get the actual progress of the file upload by using the getProgress() method. Actually there are 2 official ways to handle this. Using a progressbar adapterYou can use the convinient Zend_ProgressBar to get the actual progress and can display it in a simple manner to your user. To archive this, you have to add the wished Zend_ProgressBar_Adapter to getProgress() when you are calling it the first time. For details about the right adapter to use, look into the chapter Zend_ProgressBar Standard Adapters. Example #7 Using the progressbar adapter to retrieve the actual state
The complete handling is done by getProgress() for you in the background. Using getProgress() manuallyYou can also work manually with getProgress() without the usage of Zend_ProgressBar. Call getProgress() without settings. It will return you an array with several keys. They differ according to the used PHP extension. But the following keys are given independently of the extension:
All other returned keys are provided directly from the extensions and will not be checked. The following example shows a possible manual usage: Example #8 Manual usage of the file progress
|