5
votes

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

1
i dont understend you :( example? - rtyshyk
I mean try something like this /** * @Assert\All({ * @Assert\File * }) */private $files; and just do $this-> files = $files - greg0ire
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

1 Answers