title: Symfony input file
body: <p>so, i'm currently on an educationnal project to learn Object PHP with symfony. And, i'm stuck on uploading files to the DB...</p>
  
  <p>here is the Entity :</p>
  
  <pre><code>&lt;?php
  
  namespace MediaBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * Media
   *
   * @ORM\Table(name="media", indexes={@ORM\Index(name="k_genre", columns={"genre"}), @ORM\Index(name="k_createur", columns={"createur"})})
   * @ORM\Entity(repositoryClass="MediaBundle\Repository\MediaRepository")
   */
  class Media
  {
      /**
       * @var string
       *
       * @ORM\Column(name="name", type="string", length=255, nullable=false)
       */
      private $name;
  
      /**
       * @var string
       *
       * @ORM\Column(name="extension", type="string", length=3, nullable=false)
       */
      private $extension;
  
      /**
       * @var string
       *
       * @ORM\Column(name="link", type="text", nullable=true)
       */
      private $link;
  
      /**
       * @var integer
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="IDENTITY")
       */
      private $id;
  
  
      private $genre;
  
      /**
       * @var \MediaBundle\Entity\Personne
       *
       * @ORM\ManyToOne(targetEntity="MediaBundle\Entity\Personne")
       * @ORM\JoinColumns({
       *   @ORM\JoinColumn(name="createur", referencedColumnName="id")
       * })
       */
      private $createur;
  
      /**
       * @return string
       */
      public function getName()
      {
          return $this-&gt;name;
      }
  
      /**
       * @param string $name
       */
      public function setName($name)
      {
          $this-&gt;name = $name;
      }
  
      /**
       * @return string
       */
      public function getExtension()
      {
          return $this-&gt;extension;
      }
  
      /**
       * @param string $extension
       */
      public function setExtension($extension)
      {
          $this-&gt;extension = $extension;
      }
  
      /**
       * @return string
       */
      public function getLink()
      {
          return $this-&gt;link;
      }
  
      /**
       * @param string $link
       */
      public function setLink($link)
      {
          $this-&gt;link = $link;
      }
  
      /**
       * @return int
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * @param int $id
       */
      public function setId($id)
      {
          $this-&gt;id = $id;
      }
  
      /**
       * @return Genre
       */
      public function getGenre()
      {
          return $this-&gt;genre;
      }
  
      /**
       * @param Genre $genre
       */
      public function setGenre($genre)
      {
          $this-&gt;genre = $genre;
      }
  
      /**
       * @return Personne
       */
      public function getCreateur()
      {
          return $this-&gt;createur;
      }
  
      /**
       * @param Personne $createur
       */
      public function setCreateur($createur)
      {
          $this-&gt;createur = $createur;
      }
  
  }
  </code></pre>
  
  <p>Here's the form :</p>
  
  <pre><code> &lt;?php
  
  namespace MediaBundle\Form;
  
  use MediaBundle\Entity\Genre;
  use MediaBundle\Entity\Personne;
  use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\Extension\Core\Type\FileType;
  use Symfony\Component\Form\Extension\Core\Type\TextType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\OptionsResolver\OptionsResolver;
  
  class MediaType extends AbstractType
  {
      /**
       * {@inheritdoc}
       */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $uploadRequired = false;
  
          $builder-&gt;add('name', TextType::class, array('attr' =&gt;
              array(
                  'class' =&gt; "form-control col-8")))
              -&gt;add('extension', TextType::class, array('attr' =&gt;
                  array(
                      'class' =&gt; "form-control col-8")))
              -&gt;add('link', FileType::class, array('attr' =&gt;
                  array(
                      'class' =&gt; "form-control col-8",
                  )))
              -&gt;add('uploadPreview', FileType::class, array(
                  'mapped' =&gt; false,
                  'required' =&gt; $uploadRequired,
                  'attr' =&gt; array(
                      'class' =&gt; 'form-control col-8',
                      'placeholder' =&gt; 'Aperçu du médium'
                  )))
              -&gt;add('genre', EntityType::class, array(
                  'class' =&gt; Genre::class,
                  'attr' =&gt; array('class' =&gt; "form-control col-8")))
              -&gt;add('createur', EntityType::class, array(
                  'class' =&gt; Personne::class,
                  'attr' =&gt; array('class' =&gt; "form-control col-8")));
  
      }
  
      /**
       * {@inheritdoc}
       */
      public function configureOptions(OptionsResolver $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'MediaBundle\Entity\Media'
          ));
      }
  
      /**
       * {@inheritdoc}
       */
      public function getBlockPrefix()
      {
          return 'mediabundle_media';
      }
  
  
  }
  </code></pre>
  
  <p>And the controller :</p>
  
  <pre><code>    &lt;?php
  
  namespace MediaBundle\Controller;
  
  use MediaBundle\Entity\Media;
  use MediaBundle\Form\MediaType;
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;use Symfony\Component\HttpFoundation\Request;
  
  
  class MediaController extends Controller
  {
  
  
      public function indexAction()
      {
          $em = $this-&gt;getDoctrine()-&gt;getManager();
  
          $media = $em-&gt;getRepository('MediaBundle:Media')-&gt;findAll();
  
          return $this-&gt;render('@Media/admin/media/index.html.twig', array(
              'media' =&gt; $media,
          ));
      }
  
  
      public function newAction(Request $request)
      {
          $medium = new Media();
          $form = $this-&gt;createForm(MediaType::class, $medium);
          $form-&gt;handleRequest($request);
  
          if ($form-&gt;isSubmitted() &amp;&amp; $form-&gt;isValid()) {
              $em = $this-&gt;getDoctrine()-&gt;getManager();
              $em-&gt;persist($medium);
              $em-&gt;flush();
  
              return $this-&gt;redirectToRoute('media_show', array('id' =&gt; $medium-&gt;getId()));
          }
  
          return $this-&gt;render('@Media/admin/media/new.html.twig', array(
              'medium' =&gt; $medium,
              'form' =&gt; $form-&gt;createView(),
          ));
      }
  
  
      public function showAction(Media $medium)
      {
          $deleteForm = $this-&gt;createDeleteForm($medium);
  
          return $this-&gt;render('@Media/admin/media/show.html.twig', array(
              'medium' =&gt; $medium,
              'delete_form' =&gt; $deleteForm-&gt;createView(),
          ));
      }
  
      public function editAction(Request $request, Media $medium)
      {
          $deleteForm = $this-&gt;createDeleteForm($medium);
          $editForm = $this-&gt;createForm(MediaType::class, $medium);
          $editForm-&gt;handleRequest($request);
  
          if ($editForm-&gt;isSubmitted() &amp;&amp; $editForm-&gt;isValid()) {
              $this-&gt;getDoctrine()-&gt;getManager()-&gt;flush();
  
              return $this-&gt;redirectToRoute('media_edit', array('id' =&gt; $medium-&gt;getId()));
          }
  
          return $this-&gt;render('@Media/admin/media/edit.html.twig', array(
              'medium' =&gt; $medium,
              'edit_form' =&gt; $editForm-&gt;createView(),
              'delete_form' =&gt; $deleteForm-&gt;createView(),
          ));
      }
  
      public function deleteAction(Request $request, Media $medium)
      {
          $form = $this-&gt;createDeleteForm($medium);
          $form-&gt;handleRequest($request);
  
          if ($form-&gt;isSubmitted() &amp;&amp; $form-&gt;isValid()) {
              $em = $this-&gt;getDoctrine()-&gt;getManager();
              $em-&gt;remove($medium);
              $em-&gt;flush();
          }
  
          return $this-&gt;redirectToRoute('media_index');
      }
  
  
      private function createDeleteForm(Media $medium)
      {
          return $this-&gt;createFormBuilder()
              -&gt;setAction($this-&gt;generateUrl('media_delete', array('id' =&gt; $medium-&gt;getId())))
              -&gt;setMethod('DELETE')
              -&gt;getForm()
          ;
      }
  }
  </code></pre>
  
  <p>I have to put 2 assets, like an image + a media file, to put them in the same field in my DB. I don't have the rights to change the DB.. so, it complicates a bit things for me.</p>
  
  <p>Here is the full state :</p>
  
  <p><em>Edit the Media Entity form so you can add 2 files.
  A file for the media thumbnail
  A file for the media (audio or video)
  The change must not modify the database
  The extension field is used to store the extension of the audio or video file
  The possible extensions (mp3, wav, ogg, mp4, ogv, ...) in audio and video must be stored in parameters (yml)</em></p>