title: symfony2 image multiple upload
body: <p>I'm trying to figure out how to do multiple file uploads but without luck. I have not been able to get too much info on this.. Maybe someone here will help me out? :D</p>
  
  <p>Criteria: </p>
  
  <p>I know that in my form type I'm supposed to use multiples for fields; but when I add it, it gives me the this error.</p>
  
  <blockquote>
    <p>Catchable Fatal Error: Argument 1 passed to
    PhotoGalleryBundle\Entity\Image::setFile() must be an instance of
    Symfony\Component\HttpFoundation\File\UploadedFile, array given,
    called in
    /home/action/workspace/www/DecorInterior/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php
    on line 442 and defined 
        Image Entity</p>
  </blockquote>
  
  <p>Here is my code in PHP:</p>
  
  <pre><code>&lt;?php
  
  namespace PhotoGalleryBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Symfony\Component\Validator\Constraints as Assert;
  use Symfony\Component\HttpFoundation\File\UploadedFile;
  use Gedmo\Mapping\Annotation as Gedmo;
  
  /**
   * Image
   *
   * @ORM\Table()
   * @ORM\Entity(repositoryClass="PhotoGalleryBundle\Entity\ImageRepository")
   * @ORM\HasLifecycleCallbacks
   */
  class Image
  {
      /**
       * @var integer
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @var string
       *
       * @ORM\Column(name="imageCaptation", type="string", length=255)
       * @Assert\NotBlank
       */
      private $imageCaptation;
  
      /**
       * @var string
       *
       * @ORM\Column(name="imageName", type="string", length=255)
       */
      private $imageName;
  
      /**
       * @var string
       *
       * @ORM\Column(name="imageFilePath", type="string", length=255, nullable=true)
       */
      private $imageFilePath;
  
      /**
       * @var \DateTime
       *
       * @ORM\Column(name="imageUploadedDate", type="datetime")
       * @Gedmo\Timestampable(on="create")
       */
      private $imageUploadedDate;
  
      /**
       * @Assert\File(maxSize="6000000")
       */
      private $file;
  
      /**
       * @ORM\ManyToOne(targetEntity="Album", inversedBy="images")
       * @ORM\JoinColumn(name="album", referencedColumnName="id")
       */
      private $album;
  
      private $temp;
  
      /**
       * Get id
       *
       * @return integer 
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set imageCaptation
       *
       * @param string $imageCaptation
       * @return Image
       */
      public function setImageCaptation($imageCaptation)
      {
          $this-&gt;imageCaptation = $imageCaptation;
  
          return $this;
      }
  
      /**
       * Get imageCaptation
       *
       * @return string 
       */
      public function getImageCaptation()
      {
          return $this-&gt;imageCaptation;
      }
  
      /**
       * Set imageName
       *
       * @param string $imageName
       * @return Image
       */
      public function setImageName($imageName)
      {
          $this-&gt;imageName = $imageName;
  
          return $this;
      }
  
      /**
       * Get imageName
       *
       * @return string 
       */
      public function getImageName()
      {
          return $this-&gt;imageName;
      }
  
      /**
       * Set imageFilePath
       *
       * @param string $imageFilePath
       * @return Image
       */
      public function setImageFilePath($imageFilePath)
      {
          $this-&gt;imageFilePath = $imageFilePath;
  
          return $this;
      }
  
      /**
       * Get imageFilePath
       *
       * @return string 
       */
      public function getImageFilePath()
      {
          return $this-&gt;imageFilePath;
      }
  
      /**
       * Get imageUploadedDate
       *
       * @return \DateTime 
       */
      public function getImageUploadedDate()
      {
          return $this-&gt;imageUploadedDate;
      }
  
      /**
       * Set file.
       * 
       * @param UploadedFile $file
       */
      public function setFile(UploadedFile $file = null) {
          $this-&gt;file = $file;
          // check if we have an old image path
          if (isset($this-&gt;imageFilePath)) {
              // store the old name to delete after the update
              $this-&gt;temp = $this-&gt;imageFilePath;
              $this-&gt;imageFilePath = null;
          } else {
              $this-&gt;imageFilePath = 'initial';
          }
      }
  
      /**
       * @ORM\PrePersist()
       * @ORM\PreUpdate
       */
      public function preUpload() {
          if (null !== $this-&gt;getFile()) {
              // do whatever you want to generate a unique name
              $fileName = sha1(uniqid(mt_rand(), true));
              $this-&gt;imageFilePath = $fileName . '.' . $this-&gt;getFile()-&gt;guessExtension();
          }
      }
  
      /**
       * @ORM\PostPersist()
       * @ORM\PostUpdate
       */
      public function upload() {
          if (null === $this-&gt;getFile()) {
              return;
          }
  
          // if there is an error when moving the file, an exception will
          // be automatically thrown by move(). This will properly prevent
          // the entity from being persisted to the database on error
          $this-&gt;getFile()-&gt;move($this-&gt;getUploadRootDir(), $this-&gt;imageFilePath);
  
          // check if we have an old image
          if (isset($this-&gt;temp)) {
              // delete the old image
              unlink($this-&gt;getUploadRootDir() . '/' . $this-&gt;temp);
              // clear the temp image path
              $this-&gt;temp = null;
          }
  
          $this-&gt;imageFilePath = null;
      }
  
      /**
       * @ORM\PostRemove()
       */
      public function removeUpload() {
          $file = $this-&gt;getAbsolutePath();
          if ($file) {
              unlink($file);
          }
      }
  
      /**
       * Get file.
       * 
       * @return UploadedFile
       */
      public function getFile() {
          return $this-&gt;file;
      }
  
      public function getAbsolutePath() {
          return null === $this-&gt;imageFilePath
              ? null
              : $this-&gt;getUploadRootDir() . '/' . $this-&gt;imageFilePath;
      }
  
      public function getWebPath() {
          return null === $this-&gt;imageFilePath
              ? null
              : $this-&gt;getUploadDir() . '/' . $this-&gt;imageFilePath;
      }
      public function getUploadRootDir() {
          // the absolute path where uploaded
          // documents should be saved
          return __DIR__.'/../../../web/' . $this-&gt;getUploadDir();
      }
  
      public function getUploadDir() {
          // get rid of the __DIR__ so it doesn't screw up
          // when displaying uploaded doc/image in the view
          return 'uploads/images';
      }
  
      /**
       * Set album
       *
       * @param \PhotoGalleryBundle\Entity\Album $album
       * @return Image
       */
      public function setAlbum(\PhotoGalleryBundle\Entity\Album $album = null)
      {
          $this-&gt;album = $album;
  
          return $this;
      }
  
      /**
       * Get album
       *
       * @return \PhotoGalleryBundle\Entity\Album 
       */
      public function getAlbum()
      {
          return $this-&gt;album;
      }
  }
  </code></pre>
  
  <p>ImageType:</p>
  
  <pre><code>&lt;?php
  
  namespace PhotoGalleryBundle\Form;
  
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  
  class ImageType extends AbstractType
  {
      /**
       * @param FormBuilderInterface $builder
       * @param array $options
       */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;add('imageCaptation')
              -&gt;add('imageName')
              -&gt;add('file', 'file', array('multiple' =&gt; TRUE))
              -&gt;add('album')
          ;
      }
  
      /**
       * @param OptionsResolverInterface $resolver
       */
      public function setDefaultOptions(OptionsResolverInterface $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'PhotoGalleryBundle\Entity\Image'
          ));
      }
  
      /**
       * @return string
       */
      public function getName()
      {
          return 'photogallerybundle_image';
      }
  }
  </code></pre>