0

Documentation on this subject is rather scarce, and I can't find anything other than their documentation (http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html) on how to save just the relative path in the database and have the actual image on the webserver.

/**
 * @ORM\Column(name="photo", type="blob", nullable=true)
 */
private $photo;

Do you declare that in your entity and generate getter/setters like you would with a regular variable? How do you get the data from the POST request in your controller and call the model function in order to save the BLOB to the database?

Thanks.

bamtheboozle
  • 5,847
  • 2
  • 17
  • 31
  • There's a new feature in v3.1 that would help with what you're doing: [New in Symfony 3.1: Data URI Normalizer](https://symfony.com/blog/new-in-symfony-3-1-data-uri-normalizer) – Yoshi Apr 21 '16 at 07:18

2 Answers2

0

I never tried but from doctrine documentation:

Values retrieved from the database are always converted to PHP’s resource type or null if no data is present.

Combined to other information found there:

$stream = fopen($blobFromDcotrine,'rb');
stream_get_contents($stream);
Community
  • 1
  • 1
goto
  • 7,908
  • 10
  • 48
  • 58
0

I have solved it by doing this in my model, where 'file' is the name you give to the file when executing the POST request, and 'tmp_name' is the temp file path given to your file by PHP:

$content = file_get_contents($_FILES['file']['tmp_name']);
$user->setUserPhoto($content);
$this->em->persist($user);
$this->em->flush();

And then in the controller you return a response:

return new Response(stream_get_contents($users->getUserPhoto($user)), Response::HTTP_OK, array(
        'Content-Type' => 'application/octet-stream'));
bamtheboozle
  • 5,847
  • 2
  • 17
  • 31