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') 但是我上传文件时收到此错误:
您必须将图像文件内容保存为二进制文件
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)); }
SplFileInfo具有函数 这是为了防止您不想将文件上传到服务器,以执行该操作follow these steps. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |