public
Last active

ZF2 File Upload PR Examples

  • Download Gist
FileUploadForm.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
<?php
 
namespace Application\Form;
 
use Zend\Form\Form;
use Zend\Form\Element;
 
class FileUploadForm extends Form
{
public function __construct($name = null, $options = array())
{
parent::__construct($name, $options);
 
$myFile = new Element\File('my-file');
$myFile
->setOptions(array())
->setLabel('Single File');
$this->add($myFile);
 
$multiFile = new Element\File('multi');
$multiFile
->setOptions(array())
->setLabel('Multi File');
 
$fileCollection = new Element\Collection('collection');
$fileCollection->setOptions(array(
'count' => 2,
'allow_add' => false,
'allow_remove' => false,
'target_element' => $multiFile,
));
$this->add($fileCollection);
 
$csrf = new Element\Csrf('csrf');
$this->add($csrf);
}
}
TestController.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
<?php
 
namespace Application\Controller;
 
use Application\Form\MultiCheckboxForm;
 
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Application\Form;
use Zend\Stdlib\ErrorHandler;
 
class TestController extends AbstractActionController
{
public function formUploadAction()
{
$isValid = false;
 
$form = new Form\FileUploadForm('file-form');
$form->setAttributes(array(
'action' => $this->url()->fromRoute(
'application/default',
array('controller' => 'test', 'action' => 'form-upload')
),
));
 
if ($this->getRequest()->isPost()) {
// Postback
$data = array_merge(
$this->getRequest()->getPost()->toArray(),
$this->getRequest()->getFiles()->toArray()
);
 
$form->setData($data);
if ($form->isValid()) {
$data = $form->getData();
$isValid = true;
 
// You can do your own move, or use Zend\Validator\File\Rename
if (!empty($data['my-file'])) {
ErrorHandler::start();
move_uploaded_file($data['my-file'], '/private/tmp/my-test-file');
ErrorHandler::stop(true);
}
}
} else {
$data = array();
}
 
$view = new ViewModel(array(
'form' => $form,
'data' => $data,
'isValid' => $isValid,
));
return $view;
}
}
form-upload.phtml
HTML+PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
<div class="row">
<div class="span12">
<h2>Form Tests</h2>
 
<?php
// Init Form
$form = $this->form;
$form->prepare();
?>
<?php echo $this->form()->openTag($form); ?>
<?php echo $this->formHidden($form->get('csrf')); ?>
<div>
<?php $elem = $form->get('my-file'); ?>
<?php echo $this->formLabel($elem); ?>
<?php echo $this->formFile($elem); ?>
<?php echo $this->formElementErrors($elem); ?>
</div>
 
<?php $collection = $form->get('collection'); ?>
<?php foreach ($collection as $elem) { ?>
<div>
<?php echo $this->formLabel($elem); ?>
<?php echo $this->formFile($elem); ?>
<?php echo $this->formElementErrors($elem); ?>
</div>
<?php } ?>
 
<button class="btn btn-primary">Submit</button>
 
<?php echo $this->form()->closeTag($form); ?>
 
<?php
if (!empty($this->data)) {
?><h3><?php echo ($this->isValid) ? 'Valid Data' : 'Invalid Data'; ?></h3>
<pre><?php
print_r($this->data);
?></pre><?php
}
?>
</div>
</div>
gistfile1.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<?php
 
// Additional File Input Filter example
$inputFilter->add(
array(
'type' => 'Zend\InputFilter\FileInput',
'name' => 'my-file',
'required' => false,
'filters' => array(
array('name' => 'filerename', 'options' => array(
'target' => '/private/tmp/upload-test',
)),
array('name' => 'filelowercase'),
),
'validators' => array(
array('name' => 'fileupload'),
array('name' => 'filesize', 'options' => array(
'min' => 4000, 'max' => 5000,
)),
),
)
);

Is it possible to make the target file path dynamic. Have a drop down list on the upload form that contains a list of folders...or albums... and have the files uploaded to the specified folder?

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.