title: Symfony3 multiple upload files
body: <p>I implemented a form for upload a file in a directory. I would use a multiple insert without manytomany relation.</p>
  
  <p>This is my entity Document:
      
  
  <pre><code> namespace AppBundle\Entity;
  
   use Doctrine\ORM\Mapping as ORM;
   use Symfony\Component\Validator\Constraints as Assert;
   use Symfony\Component\HttpFoundation\File\UploadedFile;
  /**
  * @ORM\Entity
  */
  class Document
  {
  /**
   * @ORM\Id
   * @ORM\Column(type="integer")
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  public $id;
  
  /**
   * @Assert\File(maxSize="6000000")
   */
  private $file;
  
  /**
   * @ORM\Column(type="string", length=255)
   * @Assert\NotBlank
   */
  public $name;
  
  /**
   * @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()
  {
      // the absolute directory path where uploaded
      // documents should be saved
      return __DIR__.'/../../../web/'.$this-&gt;getUploadDir();
  }
  
  protected function getUploadDir()
  {
      // get rid of the __DIR__ so it doesn't screw up
      // when displaying uploaded doc/image in the view.
      return 'uploads/documents';
  }
  
   /**
   * Sets file.
   *
   * @param UploadedFile $file
   */
  public function setFile(UploadedFile $file = null)
  {
      $this-&gt;file = $file;
  }
  
  /**
   * Get file.
   *
   * @return UploadedFile
   */
  public function getFile()
  {
      return $this-&gt;file;
  }
  
  /**
   * Get id
   *
   * @return integer
   */
  public function getId()
  {
      return $this-&gt;id;
  }
  
  /**
   * Set name
   *
   * @param string $name
   *
   * @return Document
   */
  public function setName($name)
  {
      $this-&gt;name = $name;
  
      return $this;
  }
  
  /**
   * Get name
   *
   * @return string
   */
  public function getName()
  {
      return $this-&gt;name;
  }
  
  /**
   * Set path
   *
   * @param string $path
   *
   * @return Document
   */
  public function setPath($path)
  {
      $this-&gt;path = $path;
  
      return $this;
  }
  
  /**
   * Get path
   *
   * @return string
   */
  public function getPath()
  {
      return $this-&gt;path;
  }
  
  public function upload() {
      // the file property can be empty if the field is not required
      if (null === $this-&gt;getFile()) {
          return;
      }
  
      // use the original file name here but you should
      // sanitize it at least to avoid any security issues
      // move takes the target directory and then the
      // target filename to move to
      $this-&gt;getFile()-&gt;move(
              $this-&gt;getUploadRootDir(), $this-&gt;getFile()-&gt;getClientOriginalName()
      );
  
      // set the path property to the filename where you've saved the file
      $this-&gt;path = $this-&gt;getUploadRootDir()."/".$this-&gt;getFile()-&gt;getClientOriginalName();
      // clean up the file property as you won't need it anymore
      $this-&gt;file = null;
  }
  }
  </code></pre>
  
  <p>And this is my controller:</p>
  
  <pre><code>public function uploadAction(Request $request) {
      $document = new Document();
      $form = $this-&gt;createFormBuilder($document)
              -&gt;add('name')
              -&gt;add('file',FileType::class,array(
                  "attr" =&gt; array(
                      "accept" =&gt; "image/*",
                      "multiple" =&gt; "multiple",
                  )
              ))
              -&gt;add('save', SubmitType::class, array(
                  'label' =&gt; 'Salva',
                  'attr' =&gt; array('class' =&gt; 'btn btn-primary')
              ))
              -&gt;getForm();
      $form-&gt;handleRequest($request);
  
      if ($form-&gt;isValid()) {
          $em = $this-&gt;getDoctrine()-&gt;getManager();
          $document-&gt;upload();
          $em-&gt;persist($document);
          $em-&gt;flush();
  
          return $this-&gt;redirectToRoute('immovable_upload');
      }
  
      return $this-&gt;render('AppBundle:Immovable:upload.html.twig', array(
                  'form' =&gt; $form-&gt;createView(),
      ));
  }
  </code></pre>
  
  <p>How to edit my code for insert more image?? It would be enough to have a repeater of the form so that it can only do multiple insertions??</p>