As of Zend Framework 1.6 release. A new Zend_Form element has been interduced. The File element which can be used for uploading file
Here is how we can use Zend_Form_Element_File for uploading files
It is recommended that you take a look at Rob Allen tutorial (Here) on Zend Framework as this blog will depend heavily on it
First Create a new File on
/application/models/
and name it TestForm.php
Here is the content of the file
class TestForm extends Zend_Form
{
public function __construct($options = null)
{
parent::__construct($options);
$this->setAttrib('enctype', 'multipart/form-data');
$this->setName('test_form');
$element = new Zend_Form_Element_File('file_control');
$element->setLabel('Upload an image:')
->setDestination('/var/www/images')
->addValidator('Count', false, 1) // ensure only 1 file
->addValidator('Size', false, 502400) // limit to 100K
->addValidator('Extension', false, 'jpg,png,gif'); // only JPEG, PNG, and GIFs
$this->addElement($element, 'foo');
$submit = new Zend_Form_Element_Submit('submit');
$submit->setAttrib('id', 'submitbutton');
$this->addElement($submit, 'submit');
}
}
Now Create a new file on
/application/controllers/
and name it TestController.php
here is the code for the upload controller
class TestController extends Zend_Controller_Action {
function indexAction() {
$this->view->title = "Upload Test";
$form = new TestForm();
$this->view->form = $form;
if($this->_request->isPost()) {
$formData = $this->_request->getPost();
if ($form->isValid($formData)) {
$adapter = new Zend_File_Transfer_Adapter_Http();
$adapter->setDestination('/var/www/images');
if (!$adapter->receive()) {
$messages = $adapter->getMessages();
echo implode("\n", $messages);
}
}
}
}
}
and done
the URL now will be based on your install and it should be http://yourserver/yourwebroot/test/
you will find a upload control you can use for file upload
you may notice some duplication on the seDestination Method
i need to do a little investigation on this as i tested the example listed on the Zend official docs and it does not work with me.