当我在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
类中的这种形式:
$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/php1w6Fb
是的,这很正常。
我建议您阅读Symfony官方文档中关于文件上传的这一部分。
您的文件已上载到/tmp
。如果您直接将它发送到DB,而没有将它存储在另一个目录中,它就会丢失。
官方文档:http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html
它向您展示了如何存储…