当我在SonataAdminBundle中加载文件时,它们会被加载到tmp文件夹中 我有这个实体:
/**
*
* @var string
*
* @ORM\Column(type="text", length=255, nullable=false)
*/
protected $path;
/**
* @var File
*
* @Assert\File(
* maxSize = "5M",
* mimeTypes = {"image/jpeg", "image/gif", "image/png", "image/tiff"},
* maxSizeMessage = "The maxmimum allowed file size is 5MB.",
* mimeTypesMessage = "Only the filetypes image are allowed."
* )
*/
protected $file;
/**
* @return string
*/
public function getPath()
{
return $this->path;
}
/**
* @param string $path
*/
public function setPath($path)
{
$this->path = $path;
}
/**
* @return File
*/
public function getFile()
{
return $this->file;
}
/**
* @param File $file
*/
public function setFile($file)
{
$this->file = $file;
}
/**
*
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
if (null !== $this->file) {
// do whatever you want to generate a unique name
$filename = sha1(uniqid(mt_rand(), true));
$this->path = $filename.'.'.$this->file->guessExtension();
}
}
/**
*
* @ORM\PreRemove()
*/
public function removeUpload()
{
if ($file = $this->getAbsolutePath()) {
unlink($file);
}
}
/**
* Called after entity persistence
*
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
if (null === $this->file) {
return;
}
$this->file->move(
$this->getUploadRootDir(),
$this->path
);
$this->path = $this->file->getClientOriginalName();
$this->file = null;
}
这个表单在Admin
class:
$formMapper
->add('name', 'text', [
'label' => 'Name'
])
->add('address', 'text', [
'label' => 'Address'
])
->add('description', 'text', [
'label' => 'Description'
])
->add('file', 'file', [
'label' => 'Image',
'data_class' => null
])
;
当我在管理面板中加载文件,然后查看数据库,然后列path
:/ tmp / php1w6Fvb
答案 0 :(得分:1)
是的,这是正常的。
我建议您阅读关于文件上传的官方 Symfony文档的这一部分。
您的文件已上传到/tmp
。如果你直接将它发送到数据库而不将其存储在另一个目录中,它就会丢失。
官方文档: http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html
它向您展示如何存储它......