Error - Display an image saved in the BLOB database

Asked

Viewed 196 times

1

I have a php script that already does the insertion of images in the database, as shown below:

<?php
session_start();
include "conexao.inc.php";
setlocale( LC_ALL, 'pt_BR', 'pt_BR.iso-8859-1', 'pt_BR.utf-8', 'portuguese' ); 
date_default_timezone_set( 'America/Fortaleza' );
$titulo = $_POST['titulo'];
$expiro = $_POST['expiro'];
$legenda = $_POST['legenda'];
$imagem = $_FILES['imag']['tmp_name'];
$tamanho = $_FILES['imag']['size'];
$tipo = $_FILES['imag']['type'];
$nome = $_FILES['imag']['name'];
if($imagem != "none"){
    $fp = fopen($imagem, "rb");
    $conteudo = fread($fp, $tamanho);
    $conteudo = addslashes($conteudo);
    fclose($fp);
    $sql = $conn -> query("insert into slide values('$titulo','$legenda','$conteudo','$expiro','".date('y-m-d')."')");
    if($sql){
        header("location: index.php");
    }
}?>

The insertion code works and it can be viewed in the database in this way:

inserir a descrição da imagem aqui

The viewing code is below:

<?php session_start();
include "conexao.inc.php";
$sql = $conn -> query("select * from slide order by data_cri");    
while($linha = $sql -> fetch(PDO::FETCH_ASSOC)){
       if($cont==0){
         echo "<div class='item active'><center><figure><figcaption align='center'><label>".$linha['Título']."</label></figcaption><img src='".$linha['imagem']."' alt='Erro, manutenção necessária!' /></figure></center></div>";
       }else{
          echo "<div class='item'><center><figure><figcaption align='center'><label>".$linha['Título']."</label></figcaption><img src=".$linha['imagem']." alt='Erro, manutenção necessária!' /></figure></center></div>";
       }
}?>

The code of the related file.inc.php is this:

<?php
try{
$conn = new PDO('mysql:host=localhost;dbname=intranet', 'root', 'aniger_2017');
$conn -> exec("SET CHARACTER SET utf8");
}
catch(PDOException $e){
    echo "<script> alert('Erro na conexão!'); </script>";
}   ?>

The way the image is shown in the system is as follows:

inserir a descrição da imagem aqui

1 answer

0


Try Find in Base64 before showing it:

<img src="data:image/jpeg;base64,".base64_encode($linha['imagem'])." alt='Erro, manutenção necessária!' />
  • It now does not show the image, it shows the alt='Error, necessary maintenance!'

Browser other questions tagged

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