6

How to use multiple files upload in symfony? I'm not really sure how to do it 'the symfony way'. I read How to handle File Uploads with Doctrine, but how upload many files?

I try use collection field type and add setFiles method to object model

public function setFiles($files)
{
    $this->files = $files;
    foreach ($files as $file) {
        $file->setObject($this);
    }
}

but have exception (this always worked for normal model without any files)

Fatal error: Call to undefined method Symfony\Component\HttpFoundation\File\UploadedFile::setObject()

please help me.

UPDATED: I use for main object second form with file, and...

      $files = $this->getRequest()->files->get('val_image');
      foreach($files as $file){
            $foto = new Foto;
            $foto->setFile($file);
            $foto->setPremises($premises->getPremises());
            $this->getEm()->persist($foto);
        }

it's works :)

3
  • i dont understend you :( example? Commented Nov 29, 2011 at 10:44
  • I mean try something like this /** * @Assert\All({ * @Assert\File * }) */private $files; and just do $this-> files = $files Commented Nov 29, 2011 at 10:48
  • Create for file a spearate entity and then attach it like a one to many relation. e.g MyEntity has a field $photos, and this is collections of a FileEntity. I can give you a bigger example if you need Commented Aug 2, 2013 at 7:12

1 Answer 1

0

Maybe this plugin can help you:

https://github.com/caoimhin/Symfony-jQuery-File-Upload/blob/master/Resources/public/README.md

Sign up to request clarification or add additional context in comments.

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.