La nostra form
Codice php:
class Form_MyForm extends Zend_Form
{
const N_UPLOADS = 4;
public function init()
{
$this->setMethod('post');
$this->setAttrib('enctype', 'multipart/form-data');
$this->setAttrib('id','myform');
$this->setAttrib('class','myform');
// Nome
$this->addElement('text', 'nome', array(
'required' => true,
'maxlength' => 255,
'label' => 'Nome :',
'title' => 'nome',
'filters' => array('StripTags','StringTrim'),
'validators' => array(
array('StringLength',false,array(3,25)))
));
// Directory di destinazione
$uploadDir = realpath(dirname(__FILE__) . '/../../uploads').'/';
// Upload Images
foreach(range(1,self::N_UPLOADS) as $k => $v){
$options = (1===$v)? array(
'required' => true,
'ignore' => true,
'title' => 'Foto',
'class' => 'input-file',
'label' => 'Tua foto :',
'destination' => $uploadDir,
'validators' => array(
array('Count', false, self::N_UPLOADS),
array('Size', false, 2097152),
array('Extension', false, 'jpg,pjpeg'),
array('IsImage', false,'jpeg'),
)):
array(
'ignore' => true,
'title' => 'Foto',
'class' => 'input-file',
'label' => 'Tua foto',
'destination' => $uploadDir,
'validators' => array(
array('Size', false, 2097152),
array('Extension', false, 'jpg,pjpeg'),
array('IsImage', false,'jpeg'),
));
$this->addElement('file', 'photo'.$v,$options );
$upload = $this->getElement('photo'.$v);
$upload->addDecorator('Description', array('placement' => 'prepend'));
$upload->addFilter('Rename', $uploadDir.substr(sha1(uniqid(microtime())),0,10). '.jpg' );
}
//Csrf
$this->addElement('hash', 'csrf', array(
'ignore' => true
));
// Submit
$this->addElement('submit', 'ask_submit', array(
'ignore' => true,
'class' => 'ads-submit',
'label' => 'Submit',
));
}
}
Con questo codice si ha la creazione dinamica di inputs tipo file oltre al campo hidden con name MAX_FILE_SIZE (nel ns. caso 2MB), solo il primo lo rendiamo obbligatorio.
Il nostro controller
Codice php:
class IndexController extends Zend_Controller_Action
{
protected $_request;
public function init()
{
$this->_request = $this->getRequest();
}
public function indexAction()
{
$form = new Form_MyForm(array('action'=>$this->_helper->url('index')));
$extraData= array();
if ($this->_request->isPost()) {
if ($form->isValid($this->_request->getPost())){
$photos = array();
foreach(range(1,Form_MyForm::N_UPLOADS) as $k => $v){
$tmp = $form->{'photo'.$v};
if ($tmp->isUploaded()) {
if(!$tmp->receive()){
$tmp->setDescription('Invalid upload');
$this->view->form = $form;
return;
}
$photo = $this->_getFileName($tmp);
array_push($photos, $photo);
}
// Qui puoi gestire il resize
}
$extraData['photos'] = serialize($photos);
// dati da inserire nel Db
$data = array_merge($form->getValues(),$extraData);
//var_dump($data);exit();
// inserisci i dati nel db e fai il redirect
return $this->_helper->redirector('index','index');
}
}
$this->view->form = $form;
}
protected function _getFileName(Zend_Form_Element_File $file)
{
$name = $file->getFileName(null,false);
return $name;
}
}
Da notare l'uso di serialize per storare tutte le immagini nel database.
Puoi scaricare una demo qui.
Alla prossima ;) .