File Transfer, hashing and other news

Hy interested ones,

the last days I added hashing functionality to Zend_File_Transfer.
The getHash() method will return you the hash value of uploaded files.
It accepts up to 34 different hash algorithms.


Additionally I added a hash validator (Zend_Validate_File_Hash) which can be used to validate for the hash of a filecontent. For convinience Crc32, Md5 and Sha1 are also available.

$upload->addValidator('Hash', false, '3b512d52', 'crc32');

Beside that as reminder for you:
When you want to check if a file has been uploaded you can use isUploaded instead of checking the filename.

if ($upload->isUploaded('uploadform')) {...}

Some of you have also mentioned that they have not seen a complet working example with files osing Zend_Form. Well, I’m no forms expert, but here is an example which can be helpfull to see if the problem is your code, or your environment. This one works when you save it in a file and call it from your browser when your environment is configured correct:

$request = new Zend_Controller_Request_Http();
// setup the form
$form = new Zend_Form();
// file element, upload is optional, but if file is uploaded run multiple validators
$file1 = $form->createElement("file","file1");
       ->addValidator('Count', true, 2)     
       ->addValidator('Size', true, "100KB")
       ->addValidator('Extension', true, 'jpg')
// add another file element with same validators      
$file2 = clone $file1;
$submit = $form->createElement("submit","submit");
// check the form
if($request->isPost()) {
    $formData = $request->getPost();
    if($form->isValid($formData)) {
        echo "FORM VALID";
    } else {
<title>File Test</title>
<?php echo $form->render(new Zend_View());?>

After the validation was successfull you will only have to receive the files.
You can do this eighter by calling it on the adapter class

if (!$form->getTransferAdapter()->receive()) {
    print "There was a failure at receiving the files";

or by calling it on the file element

if (!$form->file1->receive()) {
    print "There was a failure at receiving file1";

but the second will only receive this single file element… so you would have to call it on every file element you want to receive.

Additionally I was told by the devteam to add the getValue() method which has also to validate and receive files. So if you call this method all of your files will be validated and received in one step… additionally you will get the filenames returned, but without the file path because of security reasons. If you need the complete path simply call getFileName().

Due to this change, which is in my eyes problematic, you should not call isValid() afterwards. The reson is that after you received the files, by getValues(), they are no longer available. So your validation will fail. You can get around this problem when you check if the file has already been received.

if (!$form->getTransferAdapter()->isReceived()) {
    if (!$form->getTransferAdapter()->receive()) {
        print "There was a failure while receiving the files";

Beware that getValues() is called by some methods of Zend_Form or their elements automatically.

Anyway, I hope you find this additional functions usefull. Feel free to test it and report problems on it to our issuetracker.

Thomas Weidner
I18N Team Leader, Zend Framework

Zend Framework Advisory Board Member
Zend Certified Engineer for Zend Framework

Back to top
  1. Mantz

    Wednesday, November 5, 2008 - 13:54:39

    Dear Thomas

    I’m quite new in Zend and I am just playing around a bit at the moment. I tryed your example, but it does not seem to work. I downloaded the newest Zend 1.6.2…
    I just simple past the error:
    Fatal error: Uncaught exception ‘Zend_File_Transfer_Exception’ with message ‘”file2” not found by file transfer adapter’ in /Applications/MAMP/htdocs/Praeptausch/library/Zend/File/Transfer/Adapter/Abstract.php:1160 Stack trace: #0 /Applications/MAMP/htdocs/Praeptausch/library/Zend/File/Transfer/Adapter/Abstract.php(564): Zend_File_Transfer_Adapter_Abstract->_getFiles(’file1′) #1 /Applications/MAMP/htdocs/Praeptausch/library/Zend/Form/Element/File.php(416): Zend_File_Transfer_Adapter_Abstract->isValid(’file1′) #2 /Applications/MAMP/htdocs/Praeptausch/library/Zend/Form.php(1985): Zend_Form_Element_File->isValid(NULL, Array) #3 /Applications/MAMP/htdocs/Praeptausch/application/views/scripts/upload/test.phtml(33): Zend_Form->isValid(Array) #4 /Applications/MAMP/htdocs/Praeptausch/library/Zend/View.php(107): include(’/Applications/M…’) #5 /Applications/MAMP/htdocs/Praeptausch/library/Zend/View/Abstract.php(787): Zend_View->_run(’../application/…’) #6 /Applications/MAMP/htdocs/Praeptausch/library/Zend/Controller/Action/Help in /Applications/MAMP/htdocs/Praeptausch/library/Zend/File/Transfer/Adapter/Abstract.php on line 1160

    Would be great if you could help me. I also tried the code from without setting required true, but again, I get an error. This time different:
    Notice: Undefined index: validators in /Applications/MAMP/htdocs/Praeptausch/library/Zend/File/Transfer/Adapter/Abstract.php on line 476

    Fatal error: Unsupported operand types in /Applications/MAMP/htdocs/Praeptausch/library/Zend/File/Transfer/Adapter/Abstract.php on line 476

    Anyway, I’m new and maybe I am doing something basic wrong. Thanx for your help.


  2. thomasw

    Wednesday, November 5, 2008 - 14:26:22

    Please ask in the mailinglists. In my blog I do not solve issues. It is not intended for this.

    Related to your post:
    When I write “I added a new feature the last days” and 1.6.2 is more than 7 weeks old, I would expect that the new features are not available in the old release ;-)

    As solution you could use trunk or wait until 1.7 is released.

  3. virtual millenium

    Friday, November 21, 2008 - 01:14:58

    Hi, good work! when will be the next release?