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 :)

rtyshyk
  • 942
  • 1
  • 15
  • 29
  • i dont understend you :( example? – rtyshyk Nov 29 '11 at 10:44
  • I mean try something like this `/** * @Assert\All({ * @Assert\File * }) */private $files; ` and just do `$this-> files = $files` – greg0ire Nov 29 '11 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 – Pawel Aug 02 '13 at 07:12

1 Answers1

0

Maybe this plugin can help you:

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

Nelson Teixeira
  • 6,297
  • 5
  • 36
  • 73