Created
September 28, 2012 04:51
-
-
Save cgmartin/3797983 to your computer and use it in GitHub Desktop.
ZF2 File Upload PR Examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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, | |
)), | |
), | |
) | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?