Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

symfony2 file upload

Tags:

upload

symfony

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 ?

like image 944
Bouki Avatar asked Aug 09 '11 15:08

Bouki


3 Answers

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();
like image 135
tuxedo25 Avatar answered Oct 25 '22 10:10

tuxedo25


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);
like image 38
Bouki Avatar answered Oct 25 '22 09:10

Bouki


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

like image 42
Maxooo Avatar answered Oct 25 '22 08:10

Maxooo