Created
March 24, 2013 16:44
-
-
Save archfizz/5232596 to your computer and use it in GitHub Desktop.
Work in progress - Symfony. 2 multiple file uploads
This file contains 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
{{ form_widget(form.post_image, { 'attr': {'multiple': 'multiple' }}) }} | |
$form = $this->createFormBuilder() | |
->add('files','file',array( | |
"attr" => array( | |
"accept" => "image/*", | |
"multiple" => "multiple", | |
) | |
)) | |
->getForm(); | |
$formView = $form->createView(); | |
$formView->getChild('files')->set('full_name', 'form[files][]'); | |
$request = $this->getRequest(); | |
if($request->getMethod() == "POST") { | |
$form->bindRequest($request); | |
$data = $form->getData(); | |
$files = $data["files"]; | |
// do stuff with your files | |
} | |
} | |
return $this->render('Bundle:Dir:index.html.twig',array("form" => $formView)); | |
} | |
<input type="file" name="name[]" multiple /> | |
class Gallery | |
{ | |
protected $id; | |
protected $name; | |
protected $description; | |
private $urlName; | |
public $files; // the array which will contain the array of Uploadedfiles | |
// GETTERS & SETTERS ... | |
public function getFiles() { | |
return $this->files; | |
} | |
public function setFiles(array $files) { | |
$this->files = $files; | |
} | |
public function __construct() { | |
$files = array(); | |
} | |
} | |
class Create extends AbstractType { | |
public function buildForm(FormBuilder $builder, array $options) { | |
$builder->add('name','text',array( | |
"label" => "Name", | |
"required" => TRUE, | |
)); | |
$builder->add('description','textarea',array( | |
"label" => "Description", | |
"required" => FALSE, | |
)); | |
$builder->add('files','file',array( | |
"label" => "Fichiers", | |
"required" => FALSE, | |
"attr" => array( | |
"accept" => "image/*", | |
"multiple" => "multiple", | |
) | |
)); | |
} | |
} | |
class GalleryController extends Controller | |
{ | |
public function createAction() { | |
$gallery = new Gallery(); | |
$form = $this->createForm(new Create(), $gallery); | |
// Altering the input field name attribute | |
$formView = $form->createView(); | |
$formView->getChild('files')->set('full_name', 'create[files][]'); | |
$request = $this->getRequest(); | |
if($request->getMethod() == "POST") | |
{ | |
$form->bindRequest($request); | |
// print "<pre>".print_r($gallery->getFiles(),1)."</pre>"; | |
if($form->isValid()) | |
{ | |
// Do what you want with your files | |
$this->get('gallery_manager')->save($gallery); | |
return $this->redirect($this->generateUrl("_gallery_overview")); | |
} | |
} | |
return $this->render("GalleryBundle:Admin:create.html.twig", array("form" => $formView)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment