php – Symfony 2:上传文件并保存为blob

前端之家收集整理的这篇文章主要介绍了php – Symfony 2:上传文件并保存为blob前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用表单和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是一个延伸0700的类,延伸到SplFileInfo

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

这是为了防止您不想将文件上传到服务器,以执行该操作follow these steps.

原文链接:https://www.f2er.com/php/136551.html

猜你在找的PHP相关文章