Reputation: 219
I just made a file upload form in my project, thanks to Symfony documentation. My project is in version 2.8.
In MySQL, I have my Media table with File for emplacement. I thought Symfony created a upload directory, but the link looks like this:
Media
+---------+----+--------------+----------------+
| Libelle | ID | Cat_Media_ID | file |
+---------+----+--------------+----------------+
| test | 1 | 1 | /tmp/phpY0oLd6 |
+---------+----+--------------+----------------+
Is it normal? Should I specify the directory where files needs to be added?
EDIT 2:
My Media Entity:
<?php
namespace AdminBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Media
*
* @ORM\Table(name="Media", indexes={@ORM\Index(name="fk_Media_Cat_Media1_idx", columns={"Cat_Media_ID"})})
* @ORM\Entity
*/
class Media
{
/**
* @var string
*
* @ORM\Column(name="Libelle", type="string", length=45, nullable=true)
*/
private $libelle;
/**
* @var integer
*
* @ORM\Column(name="ID", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var \AdminBundle\Entity\CatMedia
*
* @ORM\ManyToOne(targetEntity="AdminBundle\Entity\CatMedia")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="Cat_Media_ID", referencedColumnName="ID")
* })
*/
private $catMedia;
/**
* @ORM\Column(type="string")
*
*
* @Assert\File(mimeTypes={ "application/image" })
*/
private $file;
/**
* Set libelle
*
* @param string $libelle
* @return Media
*/
public function setLibelle($libelle)
{
$this->libelle = $libelle;
return $this;
}
/**
* Get libelle
*
* @return string
*/
public function getLibelle()
{
return $this->libelle;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set catMedia
*
* @param \AdminBundle\Entity\CatMedia $catMedia
* @return Media
*/
public function setCatMedia(\AdminBundle\Entity\CatMedia $catMedia = null)
{
$this->catMedia = $catMedia;
return $this;
}
/**
* Get catMedia
*
* @return \AdminBundle\Entity\CatMedia
*/
public function getCatMedia()
{
return $this->catMedia;
}
public function setFile($file)
{
$this->file = $file;
return $this;
}
public function getFile()
{
return $this->file;
}
}
My Media NewAction Controller:
/**
* Creates a new Media entity.
*
* @Route("/new", name="media_new")
* @Method({"GET", "POST"})
*/
public function newAction(Request $request)
{
$media = new Media();
$form = $this->createForm('AdminBundle\Form\MediaType', $media);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// $file stores the uploaded file
/** @var Symfony\Component\HttpFoundation\File\UploadedFile $file */
$file = $media->getFile();
// Generate a unique name for the file before saving it
$fileName = md5(uniqid()).'.'.$file->guessExtension();
// Move the file to the directory where media are stored
$fileDir = $this->container->getParameter('kernel.root_dir').'/../web/uploads/';
$file->move($fileDir, $fileName);
// Update the 'media' property to store the file name
// instead of its contents
$media->setFile($fileName);
$em = $this->getDoctrine()->getManager();
$em->persist($media);
$em->flush();
return $this->redirectToRoute('media_show', array('id' => $media->getId()));
}
return $this->render('AdminBundle:media:new.html.twig', array(
'medium' => $media,
'form' => $form->createView(),
));
}
My media FormType:
<?php
namespace AdminBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\FileType;
class MediaType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('libelle')
->add('catmedia','entity', array('class'=>'AdminBundle:CatMedia',
'property'=>'libelle'))
->add('file', FileType::class, array('label' => 'Image (Png or jpg file)'))
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AdminBundle\Entity\Media'
));
}
}
and User Formtype using Media FormType:
<?php
namespace AdminBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class UtilisateurType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('nom')
->add('prenom')
->add('societe')
->add('mail')
->add('tel')
->add('password')
->add('catutilisateur','entity', array('class'=>'AdminBundle:CatUtilisateur',
'property'=>'libelle'))
->add('media', MediaType::class)
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AdminBundle\Entity\Utilisateur'
));
}
}
As you can see, i have my move Function. But I do not have the directory uploads into web/, and in the database , I have this famous
+---------+----+--------------+----------------+ | Libelle | ID | Cat_Media_ID | file | +---------+----+--------------+----------------+ | test | 1 | 1 | /tmp/phpY0oLd6 | +---------+----+--------------+----------------+
Upvotes: 2
Views: 3051
Reputation: 1249
After upload move the file to your desired directory and you will get a File
object in return.
$file = $uploadedFile->move($directory, $name);
In Symfony applications, uploaded files are objects of the UploadedFile class, which provides methods for the most common operations when dealing with uploaded files;
Upvotes: 1