title: Symfony 2.2 upload files
body: <p>It's been a while since I uploaded files with Symfony2 and it seems that everything has changed, follow the guide in <a href="http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html" rel="noreferrer">How to handle File Uploads with Doctrine</a> but is outdated and dont work.</p>
  
  <p>when i try to bind the form a get a error </p>
  
  <pre><code>Catchable Fatal Error: Argument 1 passed to Entity\Portada::setFile() must be an instance of Symfony\Component\HttpFoundation\File\UploadedFile, string given, ...
  </code></pre>
  
  <p>this is my controller</p>
  
  <pre><code>/**
   * @Route("/upload", name="documento_upload")
   * @Method("POST")
   * @Template()
   */
  public function uploadAction(Request $request)
  {
      $portada = new Portada();
      $form = $this-&gt;buildUploadForm($portada);
      $form-&gt;bind($request);
  
      if ($form-&gt;isValid()) {
          $portada-&gt;upload();
      } else {
          throw new \Exception("Hay un error en el formulario");
  
      }
  
      //...
  }
  </code></pre>
  
  <p>my entity</p>
  
  <pre><code>&lt;?php
  
  namespace MyName\MyBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Symfony\Component\HttpFoundation\File\UploadedFile;
  use Symfony\Component\Validator\Constraints as Assert;
  
  class Portada
  {
      /**
       * @Assert\File(maxSize="6000000")
       */
      private $file;
  
      public $path;
  
      /**
       * Sets file.
       *
       * @param UploadedFile $file
       */
      public function setFile(UploadedFile $file = null)
      {
          $this-&gt;file = $file;
      }
  
      public function upload()
      {
          $this-&gt;path = $this-&gt;getFile()-&gt;getClientOriginalName();
  
          $this-&gt;getFile()-&gt;move(
              $this-&gt;getUploadRootDir(),
              $this-&gt;path
          );
  
          $this-&gt;file = null;
      }
  
      /**
       * Get file.
       *
       * @return UploadedFile
       */
      public function getFile()
      {
          return $this-&gt;file;
      }
  
      public function getAbsolutePath()
      {
          return null === $this-&gt;path
              ? null
              : $this-&gt;getUploadRootDir() . DIRECTORY_SEPARATOR . $this-&gt;path;
      }
  
      public function getWebPath()
      {
          return null === $this-&gt;path
              ? null
              : $this-&gt;getUploadDir() . DIRECTORY_SEPARATOR . $this-&gt;path;
      }
  
      protected function getUploadRootDir()
      {
          return __DIR__ . '/../../../../web/'. $this-&gt;getUploadDir();
      }
  
      protected function getUploadDir()
      {
          return 'uploads/portada';
      }
  }
  </code></pre>