Molecular Man Molecular Man - 4 days ago 5
PHP Question

How to upload file using a form without an entity class

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.

Answer

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.