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.

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...