I've tried to upload file using a form without an entity class. No luck so far.
// Controller
public function uploadAction() {
$request = $this->getRequest();
$form = $this->createFormBuilder()
->add('images', 'file') // If I remove this line data is submitted correctly
->add('dir', 'text')
->getForm();
if ($request->getMethod() == 'POST') {
$request = $this->getRequest();
$form->bindRequest($request);
$data = $form->getData();
var_dump($data);
}
else
return $this->render('OverseerMainBundle:Default:form.html.twig', array(
'form' => $form->createView(),
));
}
// form.html.twig
{% block body %}
<form action="{{ path('OverseerMainBundle_upload') }}" method="post" {{ form_enctype(form) }}>
{{ form_widget(form) }}
<input type="submit" />
</form>
{% endblock %}
So far var_dump echoes:
array(2) { ["images"]=> NULL ["dir"]=> NULL }
However if I remove line ->add('images', 'file')
everything is ok:
array(1) { ["dir"]=> string(4) "test" }
P.S. I've checked html code of form and attribute enctype="multipart/form-data"
is presented. So it's not an issue.
Also I hope this would help others:
if ($form->isValid()) {
$file = $form->get('file')->getData();
$name = $file->getClientOriginalName();
$dir = __DIR__.'/../../../../web/uploads';
$file->move($dir, $name) ;
}
Have you checked that the file doesn't exceed the server maximum file size limit? The default is 2MB. I haven't used Symfony 2.0 yet so I am not sure of that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With