8

I've followed this guide : http://symfony.com/doc/current/reference/forms/types/file.html

and tested the sample here

However when I try the code, I have an error :

Call to undefined method Symfony\Component\Form\Form::move()

This is happening with the line :

$form['attachment']->move($dir, $someNewFilename);

I wonder why there is this error ?

3 Answers 3

18

This doesn't use the 'Form' class, but I have had success retrieving uploads directly from the request:

/* @var Request */
$request = $this->getRequest();

/* @var UploadedFile */
$uploadedFile = $request->files->get('upfile'); //upfile must be the value of the name attribute in the <input> tag
if (null === $uploadedFile)
    return new RedirectResponse($this->generateUrl('_upload_index'));

/* @var string*/
$filename = $uploadedFile->getPathname();
Sign up to request clarification or add additional context in comments.

Comments

17

I finally found the solution

the doc is wrong

instead of :

$form['attachment']->move($dir, $someNewFilename);

it should be :

$form['attachment']->getData()->move($dir, $someNewFilename);

2 Comments

Using Symfony 2.7 (within Silex), I'm currentlly finding that ->getData() returns just the string of the filename, so ->move() etc. don't work on it.
Ah, I had a hardcoded <form> tag and hadn't added the enctype="multipart/form-data" attribute to it; adding that returns me the UploadedFile object instead of a string!
5

Now it's better to do as explained in the official documentation : http://symfony.com/doc/2.0/cookbook/doctrine/file_uploads.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.