Molecular Man Molecular Man - 9 months ago 40
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')

if ($request->getMethod() == 'POST') {
$request = $this->getRequest();

$data = $form->getData();
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" />
{% 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
is presented. So it's not an issue.

Answer Source

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.