gpt4 book ai didi

php - Symfony 2.8 中上传的文件目录

转载 作者:行者123 更新时间:2023-11-29 02:51:35 25 4
gpt4 key购买 nike

感谢 Symfony 文档,我刚刚在我的项目中制作了一个文件上传表单。我的项目是 2.8 版本。

在 MySQL 中,我有我的媒体表和用于放置的文件。我以为 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 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(),
));
}

我的媒体形式:

<?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 的 User 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 |
+---------+----+--------------+----------------+

最佳答案

上传后将文件移动到您想要的目录,您将得到一个 File 对象作为返回。

$file = $uploadedFile->move($directory, $name);

在Symfony应用程序中,上传的文件是UploadedFile类的对象,它提供了处理上传文件时最常见的操作方法;

See Symfony Api

关于php - Symfony 2.8 中上传的文件目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35143150/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com