Symfony 2: Download file and save as blob

I am trying to save images in a database using forms and Doctrine. In my essence, I did this:

/** * @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())); } 

And I also added this to my form type:

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

But I get this error when downloading a file:

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

+8
php symfony blob doctrine
source share
1 answer

You must save the contents of the image file as binary

 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 is a class that extends File , which extends SplFileInfo

SplFileInfo has a getRealPath() function that returns the path to the temp file name.

It is simple if you do not want to upload the file to the server, to do this, follow these steps .

+6
source share

All Articles