Symfony 4 - How to save images with Formbuilder (Filetype) in Base64 in the Database(Mysql)?

Asked

Viewed 82 times

1

Hello, I’m trying to make a form where the user just inserts an image. In theory, the image should be stored as a Blob file (Base64) in the database, but Symfony is storing "tmp/php/random value" in the "photo" field of the credential table. I don’t know if something is missing, because with a return $this->json($credencial->getFoto()), the returned is actually a Base64 string.

Credential:

class Credencial {
    /**
    * @ORM\Column(name="foto", type="blob")
    */
    protected $foto;

    public getFoto() {
        return $this->foto();
    }

    public setFoto($foto) {
        $this->foto = $foto;
    }
}

Credeltype:

class CredencialType extends AbstractType {
    protected $router;

    public function __construct(RouterInterface $router) {
        $this->router = $router;
    }

    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder
        ->add('foto', FileType::class, array(
            'label' => 'Foto',
            'required' => false
        ))->add('save', SubmitType::class, array(
            'label' => 'SALVAR',
            'attr' => ['class' => 'btn btn-info']
        ));
    }
}

Credential Route Controller to add new credentials:

 /**
 * @Route("/credencial/add", name="credencial_add")
 * @Template()
 */
public function add(Request $request) {
    $credencial = new Credencial();
    $form = $this->createForm(CredencialType::class, $credencial);
    $form->handleRequest($request);

    if($form->isSubmitted() && $form->isValid()) {
        $entityManager = $this->getDoctrine()->getManager();
        $entityManager->persist($credencial);
        $entityManager->flush();

        return $this->redirectToRoute('credencial_add');
    }

    return ['form' => $form->createView()];
}

All help will be welcome, thank you so much!

  • When flush() is executed what appears in /var/log/dev.log? appears an error or says it has been saved correctly?

  • When adding a new credential, var/log/dev.log does not receive a new line. What happens is that it is saving, but the "photo" field in the database instead of receiving the string with the image in Base64, receives something like "tmp/phpsomething random"

  • I did the test again, field "photo" in the Database = "/tmp/phpr2bYka", I do not know if you need to do some conversion to Base64, but what I find strange is the fact of returning in Base64, but not saving, also in Base64 in persist

  • Okay, I was just looking at documentation, and from what I’ve seen being stored in $Credential->photo is an instance of Uploadedfile, so maybe if you try to put this before the persist: "$Credential->setFoto($form['photo']->getData());"

  • Hello friend(a)! It seems that it didn’t work "$Credential->setFoto($form['photo']->getData());" :(, still saving "tmp/phpBlabla"

  • What I find strange is that "Return $this->json($form["photo"]->getData());" is returning: "data:image/jpeg;Base64,/9j/.........................................................." as if it were a string with the data in Base64

  • Hello!!! I just ran a test with "$credential->setFoto(file_get_contents($form["photo"]->getData());" and it worked perfectly! Saved in the database as Base64, damn it, thank you very much, you gave me the keywords to research more about the problem!

Show 2 more comments

1 answer

1


Really what $form["foto"] is an instance of Uploadedfile, such as fajuchem said. the return $this->json($form["foto"]->getData()); performed the conversion automatically to string, so returned the Base64 string of the image, and to save in the Database it is necessary to use file_get_contents, which reads the contents of a file and receives as a parameter the directory of that file, which by the way, was what the $form["foto"] owned and was storing it in the BD.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.