由于Symfony文档,我刚刚在我的项目中创建了一个文件上传表单。 我的项目是2.8版本。
在MySQL中,我有我的Media表,文件用于安置。我以为Symfony创建了一个上传目录,但链接看起来像这样:
媒体
+---------+----+--------------+----------------+
| Libelle | ID | Cat_Media_ID | file |
+---------+----+--------------+----------------+
| test | 1 | 1 | /tmp/phpY0oLd6 |
+---------+----+--------------+----------------+
这是正常的吗?我应该指定需要添加文件的目录吗?
编辑2:
我的媒体实体:
<?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;
}
}
我的媒体NewAction控制器:
/**
* 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(),
));
}
我的媒体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'
));
}
}
和使用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'
));
}
}
正如你所看到的,我有我的移动功能。但我没有将目录上传到web /,在数据库中,我有这个着名的
+---------+----+--------------+----------------+ | Libelle | ID | Cat_Media_ID | file | +---------+----+--------------+----------------+ | test | 1 | 1 | /tmp/phpY0oLd6 | +---------+----+--------------+----------------+
答案 0 :(得分:1)
上传后将文件移动到所需的目录,然后您将获得一个File
对象。
$file = $uploadedFile->move($directory, $name);
在Symfony应用程序中,上传的文件是UploadedFile类的对象,它提供了处理上传文件时最常见操作的方法;