加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 13:09:58 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试使用表单和Doctrine在数据库中保存图像.在我的实体中,我做到了这一点: /** * @ORMColumn(name="photo",type="blob",nullable=true) */private $photo;private $file;/** * @ORMPrePersist() * @ORMPreUpdate() */public function upload(){ if
我正在尝试使用表单和Doctrine在数据库中保存图像.在我的实体中,我做到了这一点:
/**
 * @ORMColumn(name="photo",type="blob",nullable=true)
 */
private $photo;

private $file;


/**
 * @ORMPrePersist()
 * @ORMPreUpdate()
 */
public function upload()
{
    if (null === $this->file) {
        return;
    }

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

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

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

但是我上传文件时收到此错误:

Serialization of ‘SymfonyComponentHttpFoundationFileUploadedFile’
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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读