This is for a very old version of Symfony – 2.3
It might not work with the version of Symfony you’re using
Here is how I do it.
What it does? Upload an image file and an audio file and add an encode job.
The purpose is to join an image and a mp3 into a mkv file x264 encoded with ffmpeg.
But this here is only to upload the files and add the related entities. Yes I am very proud of myself for having accomplished this even if it was extremely more complicated than just doing it in PHP.
Entities:
Encodejob
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
<?php namespace Dalu\MediaBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\HttpFoundation\File\UploadedFile; /** * @ORM\Entity */ class Encodejob { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @ORM\ManyToOne(targetEntity="Image") */ private $image; /** * @ORM\ManyToOne(targetEntity="Audio") */ private $audio; /** * @ORM\Column */ private $video; /** * @ORM\Column(type="boolean") */ private $finished = false; private $uploaded_image; private $uploaded_audio; /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set video * * @param string $video * @return Encodejob */ public function setVideo($video) { $this->video = $video; return $this; } /** * Get video * * @return string */ public function getVideo() { return $this->video; } /** * Set finished * * @param boolean $finished * @return Encodejob */ public function setFinished($finished) { $this->finished = $finished; return $this; } /** * Get finished * * @return boolean */ public function getFinished() { return $this->finished; } /** * Set image * * @param \Dalu\MediaBundle\Entity\Image $image * @return Encodejob */ public function setImage(\Dalu\MediaBundle\Entity\Image $image = null) { $this->image = $image; return $this; } /** * Get image * * @return \Dalu\MediaBundle\Entity\Image */ public function getImage() { return $this->image; } /** * Set audio * * @param \Dalu\MediaBundle\Entity\Audio $audio * @return Encodejob */ public function setAudio(\Dalu\MediaBundle\Entity\Audio $audio = null) { $this->audio = $audio; return $this; } /** * Get audio * * @return \Dalu\MediaBundle\Entity\Audio */ public function getAudio() { return $this->audio; } public function setUploadedAudio(UploadedFile $uploaded_audio = null) { $this->uploaded_audio = $uploaded_audio; return $this; } public function getUploadedAudio() { return $this->uploaded_audio; } public function setUploadedImage(UploadedFile $uploaded_image = null) { $this->uploaded_image = $uploaded_image; return $this; } public function getUploadedImage() { return $this->uploaded_image; } } |
Image
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
<?php namespace Dalu\MediaBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; /** * @ORM\Entity */ class Image { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @ORM\Column * @Assert\NotBlank */ private $filename; /** * @ORM\Column(nullable=true) */ private $path; public function getAbsolutePath() { return null === $this->path ? null : $this->getUploadRootDir() . '/' . $this->path; } public function getWebPath() { return null === $this->path ? null : $this->getUploadDir() . '/' . $this->path; } protected function getUploadRootDir() { // the absolute directory path where uploaded // documents should be saved return __DIR__ . '/../../../../web/' . $this->getUploadDir(); } protected function getUploadDir() { // get rid of the __DIR__ so it doesn't screw up // when displaying uploaded doc/image in the view. return 'media/image'; } /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set filename * * @param string $filename * @return Image */ public function setFilename($filename) { $this->filename = $filename; return $this; } /** * Get filename * * @return string */ public function getFilename() { return $this->filename; } /** * Set path * * @param string $path * @return Image */ public function setPath($path) { $this->path = $path; return $this; } /** * Get path * * @return string */ public function getPath() { return $this->path; } } |
Audio
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
<?php namespace Dalu\MediaBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; /** * @ORM\Entity */ class Audio { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @ORM\Column * @Assert\NotBlank */ private $filename; /** * @ORM\Column(nullable=true) */ private $path; public function getAbsolutePath() { return null === $this->path ? null : $this->getUploadRootDir() . DIRECTORY_SEPARATOR . $this->path; } public function getWebPath() { return null === $this->path ? null : $this->getUploadDir() . '/' . $this->path; } protected function getUploadRootDir() { // the absolute directory path where uploaded // documents should be saved return __DIR__ . '/../../../../web/' . $this->getUploadDir(); } protected function getUploadDir() { // get rid of the __DIR__ so it doesn't screw up // when displaying uploaded doc/image in the view. return 'media/audio'; } /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set filename * * @param string $filename * @return Image */ public function setFilename($filename) { $this->filename = $filename; return $this; } /** * Get filename * * @return string */ public function getFilename() { return $this->filename; } /** * Set path * * @param string $path * @return Image */ public function setPath($path) { $this->path = $path; return $this; } /** * Get path * * @return string */ public function getPath() { return $this->path; } } |
Form
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<?php namespace Dalu\MediaBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; class EncodejobType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('video','text',array( 'attr' => array( 'class'=> 'input-xxlarge' ))) ->add('uploaded_image','file',array( 'attr' => array( 'class'=> 'input-xxlarge' ))) ->add('uploaded_audio','file',array( 'attr' => array( 'class'=> 'input-xxlarge' ))) ; } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'Dalu\MediaBundle\Entity\Encodejob' )); } public function getName() { return 'dalu_mediabundle_encodejobtype'; } } |
Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
<?php namespace Dalu\MediaBundle\Controller; use Dalu\MediaBundle\Entity\Audio; use Dalu\MediaBundle\Entity\Image; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; use Dalu\MediaBundle\Form\EncodejobType; use Dalu\MediaBundle\Entity\Encodejob; use Symfony\Component\HttpFoundation\Request; class EncodeController extends Controller { /** * @Route("/", name="media_encode_index") * @Template() */ public function indexAction() { $jobs = $this->getDoctrine()->getRepository('DaluMediaBundle:Encodejob')->findAll(); return array( 'jobs' => $jobs ); } /** * @Route("/new", name="media_encode_new") * @Template() */ public function newAction() { $entity = new Encodejob(); $form = $this->createForm(new EncodejobType(), $entity); return array( 'entity' => $entity, 'form' => $form->createView() ); } /** * @Route("/create", name="media_encode_create") * @Method("POST") * @Template() */ public function newpostAction(Request $request) { $entity = new Encodejob(); $form = $this->createForm(new EncodejobType(), $entity); $form->bind($request); if ($form->isValid()) { $em = $this->getDoctrine()->getManager(); $image = new Image(); $audio = new Audio(); $uploaded_image = $form->get('uploaded_image')->getData(); $uploaded_audio = $form->get('uploaded_audio')->getData(); $image->setFilename($uploaded_image->getClientOriginalName()); //TODO: Bild umbenennen $audio->setFilename($uploaded_audio->getClientOriginalName()); //TODO: Audio umbenennen $date = new \DateTime(); $image->setPath($date->format('Y/m/d')); $audio->setPath($date->format('Y/m/d')); $uploaded_image->move($image->getAbsolutePath()); $uploaded_audio->move($audio->getAbsolutePath()); $entity->setImage($image); $entity->setAudio($audio); $em->persist($image); $em->persist($audio); $em->persist($entity); $em->flush(); return $this->redirect($this->generateUrl('media_encode_index')); } return array( 'entity' => $entity, 'form' => $form ); } } |
new.html.twig
1 2 3 4 5 6 7 8 9 10 11 12 13 |
{% extends 'DaluThemeBundle::bootstrap.html.twig' %} {% block body %} <div class="container"> <div class="row"> <div class="span12"> <form action="{{ path('media_encode_create') }}" method="post" enctype="multipart/form-data"> {{ form_widget(form) }} <button type="submit" class="btn">Upload</button> </form> </div> </div> </div> {% endblock %} |
One Reply to “Symfony2 File Upload with Related Entities”