Saturday
1
November
2008

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.

$upload->getHash('uploadform');

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:

ini_set('include_path',ini_get('include_path').PATH_SEPARATOR.'../library');
require_once('Zend/Loader.php');
Zend_Loader::registerAutoload();
	
$request = new Zend_Controller_Request_Http();
	
// setup the form
$form = new Zend_Form();
$form->setMethod("post");
$form->setAttrib("enctype",Zend_Form::ENCTYPE_MULTIPART);
	
// file element, upload is optional, but if file is uploaded run multiple validators
$file1 = $form->createElement("file","file1");
$file1->setRequired(false)
       ->setLabel("File:")
       ->addValidator('Count', true, 2)     
       ->addValidator('Size', true, "100KB")
       ->addValidator('Extension', true, 'jpg')
       ->addValidator('MimeType',true,array('image/jpeg'))
       ->addValidator('ImageSize',true,array(0,0,340,480))
       ->setMultiFile(3);
	
// add another file element with same validators      
$file2 = clone $file1;
$file2->setName("file2")
       ->setMultiFile(0);
	
$submit = $form->createElement("submit","submit");
$submit->setLabel("GO!");
	
$form->addElements(array($file1,$file2,$submit));
	
// check the form
if($request->isPost()) {
    $formData = $request->getPost();
    if($form->isValid($formData)) {
        echo "FORM VALID";
    } else {
        print_r($form->getMessages());                              
    }
}
?>
<html>
<head>
<title>File Test</title>
</head>
<body>
<?php echo $form->render(new Zend_View());?>
</body>
</html>

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().

Attention:
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.

Greetings
Thomas Weidner
I18N Team Leader, Zend Framework

Zend Framework Advisory Board Member
Zend Certified Engineer for Zend Framework

Back to top