Sunday, July 13, 2008

News on Zend_File_Transfer

By thomasw at 20:01:36

Hy interested ones,

the Zend Framework has since today a new file transfer component ready for playing around.

Zend_File_Transfer allows file uploads and downloads when it’s finished.
For now, there is only HTTP POST upload available and it looks like it will be released with 1.6.

The component comes with several validators specially written for validating files with this component.
Below is an example of usage:

The example form looks like this:

<form enctype="multipart/form-data" action="index2.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
Choose a file to upload: <input name="uploadedfiles[]" type="file" /><br />
Choose a file to upload: <input name="uploadedfiles[]" type="file" /><br />
<input type="submit" value="Upload File" />
</form>

The target looks like this:

<?php
require_once "Zend/File/Transfer/Adapter/Http.php";
require_once "Zend/File/Transfer/Exception.php";
	
$adapter = new Zend_File_Transfer_Adapter_Http();
$adapter->setValidators('size', '2000');
	
if ($adapter->isValid() === false) {
    print_r($adapter->getMessages()); 
}
	
try {
    $adapter->setDestination('C:\temp')
            ->receive();
} catch (Zend_File_Transfer_Exception $e) {
    print $e->getMessage();
}
	
print "DOWNLOAD FINISHED!!";

As you see the usage is quite simple.
The seperated isValid() call is not necessary but will show you which failures occured while validation.
Any failure while uploading will also be returned as validation error even when you do not set any validator.

For now there are these validators available:
Zend_Validate_File_Count - validates the file count
Zend_Validate_File_DiskSpace - validates the disk usage of ALL files
Zend_Validate_File_Extension - validates the real file extension
Zend_Validate_File_Size - validates the size of single files

The Zend_Validate_File_Upload validator is the internal validator which checks for upload failures. There is no need to use it as it is automatically used by Zend_File_Transfer itself.

In future the component will be extended with other adapters and also with download capabilities.
I hope you enjoy testing and playing around.

Greetings
Thomas Weidner, I18N Team Leader, Zend Framework
http://www.thomasweidner.com

Calendar

  • July 2008
    SunMonTueWedThuFriSat
     12345
    6789101112
    13141516171819
    20212223242526
    2728293031 

Last 8 comments

Admin area