title: symfony2 multiple file upload
body: <blockquote>
    <p>I want to insert multiple images to each entity
    because this code it works well, but it takes the last image add only.
    Is there a way to upload all images with Symfony2 using a simple form? </p>
  </blockquote>
  
  <p>equipe entity 
     
  
  <pre><code>namespace Examen\CoupedumondeBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Symfony\Component\Validator\Constraints as Assert;
  use Symfony\Component\HttpFoundation\File\UploadedFile;
  
  /**
   * equipe
   *
   * @ORM\Table()
   * @ORM\Entity
   * @ORM\HasLifecycleCallbacks
   */
  class equipe
  {
      /**
       * @var integer
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @var string
       *
       * @ORM\Column(name="pays", type="string", length=255)
       */
      private $pays;
  
      /**
       * @var string
       *
       * @ORM\Column(name="capitaine", type="string", length=255)
       */
      private $capitaine;
  
      /**
       * @var string
       *
       * @ORM\Column(name="continent", type="string", length=255)
       */
      private $continent;
  
      /**
       * @var string
       *
       * @ORM\Column(name="coach", type="string", length=255)
       */
      private $coach;
  
      /**
       * @Assert\File(maxSize="6000000")
       */
      public $file;
  
  
      /**
       * @return mixed
       */
      public function getPath()
      {
          return $this-&gt;path;
      }
  
      /**
       * @param mixed $path
       */
      public function setPath($path)
      {
          $this-&gt;path = $path;
      }
  
  
      /**
       * Get id
       *
       * @return integer 
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set pays
       *
       * @param string $pays
       * @return equipe
       */
      public function setPays($pays)
      {
          $this-&gt;pays = $pays;
  
          return $this;
      }
  
      /**
       * Get pays
       *
       * @return string 
       */
      public function getPays()
      {
          return $this-&gt;pays;
      }
  
      /**
       * Set capitaine
       *
       * @param string $capitaine
       * @return equipe
       */
      public function setCapitaine($capitaine)
      {
          $this-&gt;capitaine = $capitaine;
  
          return $this;
      }
  
      /**
       * Get capitaine
       *
       * @return string 
       */
      public function getCapitaine()
      {
          return $this-&gt;capitaine;
      }
  
      /**
       * Set continent
       *
       * @param string $continent
       * @return equipe
       */
      public function setContinent($continent)
      {
          $this-&gt;continent = $continent;
  
          return $this;
      }
  
      /**
       * Get continent
       *
       * @return string 
       */
      public function getContinent()
      {
          return $this-&gt;continent;
      }
  
      /**
       * Set coach
       *
       * @param string $coach
       * @return equipe
       */
      public function setCoach($coach)
      {
          $this-&gt;coach = $coach;
  
          return $this;
      }
  
      /**
       * Get coach
       *
       * @return string 
       */
      public function getCoach()
      {
          return $this-&gt;coach;
      }
  
  
      /**
       * @ORM\Column(type="string", length=255, nullable=true)
       */
      public $path;
  
      public function getAbsolutePath()
      {
          return null === $this-&gt;path ? null : $this-&gt;getUploadRootDir().'/'.$this-&gt;path;
      }
  
      public function getWebPath()
      {
          return null === $this-&gt;path ? null : $this-&gt;getUploadDir().'/'.$this-&gt;path;
      }
  
      protected function getUploadRootDir()
      {
          // le chemin absolu du répertoire où les documents uploadés doivent être sauvegardés
          return __DIR__.'/../../../../web/'.$this-&gt;getUploadDir();
      }
  
      protected function getUploadDir()
      {
          // on se débarrasse de « __DIR__ » afin de ne pas avoir de problème lorsqu'on affiche
          // le document/image dans la vue.
          return 'uploads/documents';
      }
      /**
       * @ORM\PrePersist()
       * @ORM\PreUpdate()
       */
      public function preUpload()
      {
          if (null !== $this-&gt;file) {
              // faites ce que vous voulez pour générer un nom unique
              $this-&gt;path = sha1(uniqid(mt_rand(), true)).'.'.$this-&gt;file-&gt;guessExtension();
          }
      }
  
      /**
       * @ORM\PostPersist()
       * @ORM\PostUpdate()
       */
      public function upload()
      {
          if (null === $this-&gt;file) {
              return;
          }
  
          // s'il y a une erreur lors du déplacement du fichier, une exception
          // va automatiquement être lancée par la méthode move(). Cela va empêcher
          // proprement l'entité d'être persistée dans la base de données si
          // erreur il y a
          $this-&gt;file-&gt;move($this-&gt;getUploadRootDir(), $this-&gt;path);
  
          unset($this-&gt;file);
      }
  
      /**
       * @ORM\PostRemove()
       */
      public function removeUpload()
      {
          if ($file = $this-&gt;getAbsolutePath()) {
              unlink($file);
          }
      }
  
  }
  </code></pre>
  
  <p>equipeType</p>
  
  <pre><code>public function buildForm(FormBuilderInterface $builder, array $options)
  {
      $builder
          -&gt;add('pays')
          -&gt;add('capitaine')
          -&gt;add('continent')
          -&gt;add('coach')
          -&gt;add('file','file',array(
              "attr" =&gt; array(
                  "accept" =&gt; "image/*",
                  "multiple" =&gt; "multiple",
              )
          ));
  }
  </code></pre>
  
  <p>controller</p>
  
  <pre><code>public function insertionAction()
  {
      $em= $this-&gt;getDoctrine()-&gt;getManager();
      $pers = new equipe;
      $form= $this-&gt;createForm(new equipeType, $pers);
      $request= $this-&gt;container-&gt;get('request');
  
  
      if($request-&gt;getMethod()=='POST')
      {
          $form-&gt;bind($request);
          if ($form-&gt;isValid()){
              $em-&gt;persist($pers);
              $em-&gt;flush();
              return new RedirectResponse($this-&gt;get('router')-&gt;generate('examen_coupedumonde_homepage'));
          }
  
      }
  
      return $this-&gt;render('ExamenCoupedumondeBundle:Default:insertion.html.twig',array('form'=&gt;$form-&gt;createView()));
  }
  </code></pre>
  
  <p>Is there someone to help me and Thank you :)</p>