gpt4 book ai didi

php - 交响乐 2 : Upload file and save as blob

转载 作者:可可西里 更新时间:2023-11-01 12:17:19 24 4
gpt4 key购买 nike

我正在尝试使用表单和 Doctrine 将图像保存在数据库中。在我的实体中,我这样做了:

/**
* @ORM\Column(name="photo", type="blob", nullable=true)
*/
private $photo;

private $file;


/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function upload()
{
if (null === $this->file) {
return;
}

$this->setPhoto(file_get_contents($this->getFile()));
}

我还在我的表单类型中添加了这个:

->add('file', 'file')

但是当我上传文件时出现这个错误:

Serialization of 'Symfony\Component\HttpFoundation\File\UploadedFile' is not allowed

最佳答案

您必须将图像文件内容保存为二进制

public function upload()
{
if (null === $this->file) {
return;
}

//$strm = fopen($this->file,'rb');
$strm = fopen($this->file->getRealPath(),'rb');
$this->setPhoto(stream_get_contents($strm));
}

UploadedFile是一个扩展 File 的类延伸 SplFileInfo

SplFileInfo 具有函数 getRealPath()返回临时文件名的路径。

这只是为了防止您不想将文件上传到服务器,这样做follow these steps .

关于php - 交响乐 2 : Upload file and save as blob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25228932/

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