我在上传文件时遇到问题。我得到一个名为“name”的文件。但没有延期。 怎么办呢? 请帮忙 这是我的实体:
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* fichier
*
* @ORM\Table()
* @ORM\Entity
*/
class fichier
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
* @ORM\ManyToOne(targetEntity="rex\Bundle\Entity\fiche")
* @ORM\JoinColumn
*/
private $titreFiche;
/**
* @param string $titreFiche
*/
public function setTitreFiche($titreFiche)
{
$this->titreFiche = $titreFiche;
}
/**
* @return string
*/
public function getTitreFiche()
{
return $this->titreFiche;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank
*/
public $name;
/**
* @return mixed
*/
public function getName()
{
return $this->name;
}
/**
* @param mixed $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
public $path;
/**
* @param mixed $path
*/
public function setPath($path)
{
$this->path = $path;
}
/**
* @return mixed
*/
public function getPath()
{
return $this->path;
}
/**
* @Assert\File
* maxSize = "50000000",
* maxSizeMessage = "Votre fichier est trop gros ({{ size }}). La taille maximum autorisée est : {{ limit }}")
*
*/
public $file;
/**
* @param mixed $file
*/
public function setFile($file)
{
$this->file = $file;
}
/**
* @return mixed
*/
public function getFile()
{
return $this->file;
}
// Upload d'image
public function getFullImagePath()
{
return null === $this->name ? null : $this->getUploadRootDir().$this->name;
}
protected function getUploadRootDir()
{
// the absolute directory path where uploaded documents should be saved
return $this->getTmpUploadRootDir().$this->getId()."/";
}
protected function getTmpUploadRootDir()
{
// the absolute directory path where uploaded documents should be saved
return __DIR__ . '/../../../../web/uploads/';
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUploadImage()
{
if (null !== $this->file) {
// $name = sha1(uniqid(mt_rand(), true));
$this->name = 'name.'.$this->file->getClientOriginalName();
}
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function uploadImage()
{
if (null === $this->file) {
return;
}
if(!is_dir($this->getUploadRootDir())){
mkdir($this->getUploadRootDir());
}
$this->file->move($this->getUploadRootDir(), $this->name);
unset($this->file);
}
/**
* @ORM\PostRemove()
*/
public function removeImage()
{
unlink($this->getFullImagePath());
rmdir($this->getUploadRootDir());
}
}
答案 0 :(得分:0)
您没有设置生命周期声明....这是解决方案:
/*
* @ORM\Table()
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
*/
class fichier
法文文档
http://symfony.com/fr/doc/current/cookbook/doctrine/file_uploads.html
英文文档
http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html