title: Multi upload Symfony 2.3 with Dropzone
body: <p>I tried a new implementation of JQuery library : <a href="http://www.dropzonejs.com/" rel="nofollow noreferrer">dropzone.js</a></p>
  
  <p>This library provides a good multi upload interface with drag and drop solution.</p>
  
  <p>It seems that the multi upload system of Symfony isn't easy.</p>
  
  <p>I've this error :</p>
  
  <pre><code>Error: Cannot use object of type Symfony\Component\HttpFoundation\File\UploadedFile as array in /vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Csrf/EventListener/CsrfValidationListener.php line 87
  </code></pre>
  
  <p>My entity File :</p>
  
  <pre><code>/**
  * @ORM\Entity
  * @ORM\Table(name="media__file")
  * @ORM\HasLifecycleCallbacks
  */
  class File
  {
  /**
   * @ORM\Id
   * @ORM\Column(type="integer")
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  public $id;
  
  /**
   * @ORM\Column(type="string", length=255)
   * @Assert\NotBlank
   */
  public $name;
  
  /**
   * @ORM\Column(type="string", length=255, nullable=true)
   */
  public $path;
  
  /**
   * @Assert\File(maxSize="6000000")
   */
  public $file;
  
  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()
  {
  
      return __DIR__.'/../../../../../web/'.$this-&gt;getUploadDir();
  }
  
  protected function getUploadDir()
  {
  
      return 'uploads/files';
  }
  
   /**
   * @ORM\PrePersist()
   * @ORM\PreUpdate()
   */
  public function preUpload()
  {
      if (null !== $this-&gt;file) {
          $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;
      }
  
  
      $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>My controller FileController :</p>
  
  <pre><code>/**
   * @Route("/admin/media/upload")
   * @Template()
   */
  public function uploadsAction (){
  
      $file = new File();
  
      $form = $this-&gt;createForm(new \Tperroin\Bundle\MediaBundle\Form\FileUploadType(), $file);
  
      if ($this-&gt;getRequest()-&gt;isMethod('POST')) {
          $form-&gt;bind($this-&gt;getRequest());
  
          if ($form-&gt;isValid()) {
  
              $em = $this-&gt;getDoctrine()-&gt;getManager();
  
              $em-&gt;persist($file);
              $em-&gt;flush();
  
              return $this-&gt;redirect($this-&gt;generateUrl('tperroin_home_default_index'));
          }
      }
  
  return array('form' =&gt; $form-&gt;createView());
  }
  </code></pre>
  
  <p>And finally my twig template :</p>
  
  <pre><code>&lt;div class="widget-content"&gt;
  
          &lt;form action="{{ url('tperroin_media_file_uploads') }}" method="post" {{ form_enctype(form) }} class="dropzone"&gt;
  
              &lt;div class="fallback"&gt;
                  &lt;input name="file" type="file" multiple /&gt;
              &lt;/div&gt;
          &lt;/form&gt;
  
      &lt;/div&gt;
  </code></pre>
  
  <p><strong>EDIT :</strong></p>
  
  <p>FormType :</p>
  
  <pre><code>class FileUploadType extends AbstractType
  {
  public function buildForm(FormBuilderInterface $builder, array $options)
  {
      $builder-&gt;add('file', 'file');
  }
  public function getName()
  {
      return 'file';
  }
  }
  </code></pre>
  
  <p>What is wrong with my code ? I know it's a problem with UploadedFile and array but I don't know how to resolve that.</p>
  
  <p>Thank you for all !</p>
  
  <p><strong>EDIT :</strong></p>
  
  <p>Maybe this link can help someone, I fail to reproduce this :</p>
  
  <p><a href="https://stackoverflow.com/questions/10445408/problems-with-multiple-file-upload-in-symfony2">Problems With Multiple File Upload In Symfony2</a></p>
  
  <p><strong>EDIT with new uploadAction function :</strong></p>
  
  <pre><code>/**
  * @Route("/upload/process", name="upload_media")
  */
     public function uploadAction()
     {
     $request = $this-&gt;get('request');
     $files = $request-&gt;files;       
  
     $directory = $this-&gt;get('kernel')-&gt;getRootDir() . '/../web' . $this-&gt;getRequest()-&gt;getBasePath() . '/files/';
  
     foreach ($files as $uploadedFile) {
  
         $name = $uploadedFile-&gt;getClientOriginalName();
         $file = $uploadedFile-&gt;move($directory, $name);
  
         $upload = new File($name);
  
         $em = $this-&gt;get('doctrine')-&gt;getManager();
  
         $em-&gt;persist($upload);
  
         $em-&gt;flush();
  
  
     }
  
     return $this-&gt;redirect($this-&gt;generateUrl('tperroin_media_default_index'));
     }
  </code></pre>