Curso de PHP🐘 y MySql🐬 [97.- Mostrar imágenes guardadas en BLOB]
En este tutorial vamos a mostrar las imagenes que guardamos en el tutorial anterior, esto lo aremos gracias a la funcion code64 que nos ofrese php y html
Codigo: https://github.com/programadornovato/php/commit/fd69dd4f8aa25c3d77c8033dcbef9ce013e2d878
<!doctype html> <html lang="en"> <head> <title>Images</title> <!-- Required meta tags --> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <!-- Bootstrap CSS --> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> </head> <body> <div class="container mt-3"> <div class="row"> <div class="col-12"> <?php if (isset($_REQUEST['guardar'])) { if (isset($_FILES['foto']['name'])) { $tipoArchivo = $_FILES['foto']['type']; $permitido=array("image/png","image/jpeg"); if( in_array($tipoArchivo,$permitido) ==false ){ die("Archivo no permitido"); } $nombreArchivo = $_FILES['foto']['name']; $tamanoArchivo = $_FILES['foto']['size']; $imagenSubida = fopen($_FILES['foto']['tmp_name'], 'r'); $binariosImagen = fread($imagenSubida, $tamanoArchivo); include_once "db_empresa.php"; $con = mysqli_connect($db_host, $db_user, $db_pass, $db_database); $binariosImagen = mysqli_escape_string($con, $binariosImagen); $query = "INSERT INTO imagenes (nombre ,imagen ,tipo) values ('" . $nombreArchivo . "','" . $binariosImagen . "','" . $tipoArchivo . "'); "; $res = mysqli_query($con, $query); if ($res) { ?> <div class="alert alert-primary alert-dismissible fade show" role="alert"> <button type="button" class="close" data-dismiss="alert" aria-label="Close"> <span aria-hidden="true">×</span> <span class="sr-only">Close</span> </button> Registro insertado exitosamente </div> <?php } else { ?> <div class="alert alert-danger alert-dismissible fade show" role="alert"> <button type="button" class="close" data-dismiss="alert" aria-label="Close"> <span aria-hidden="true">×</span> <span class="sr-only">Close</span> </button> Error <?php echo mysqli_error($con); ?> </div> <?php } } } ?> <form method="post" enctype="multipart/form-data"> <div class="form-group"> <input type="file" class="form-control-file" name="foto"> </div> <div class="form-group"> <button type="submit" class="btn btn-primary" name="guardar">Enviar</button> </div> </form> </div> </div> </div> <!-- Optional JavaScript --> <!-- jQuery first, then Popper.js, then Bootstrap JS --> <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script> </body> </html> <!doctype html> <html lang="en"> <head> <title>Lee Imagen</title> <!-- Required meta tags --> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <!-- Bootstrap CSS --> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> </head> <body> <div class="container mt-3"> <div class="row"> <div class="col-12"> <table class="table table-striped"> <thead class="thead-inverse"> <tr> <th>Nombre</th> <th>Tipo</th> <th>Imagen</th> </tr> </thead> <tbody> <?php include_once "db_empresa.php"; $con = mysqli_connect($db_host, $db_user, $db_pass, $db_database); $query = "SELECT nombre,tipo,imagen FROM imagenes;"; $res = mysqli_query($con, $query); while ($row = mysqli_fetch_assoc($res)) { ?> <tr> <td><?php echo $row['nombre']; ?></td> <td><?php echo $row['tipo']; ?></td> <td> <img width="100" src="data:<?php echo $row['tipo']; ?>;base64,<?php echo base64_encode($row['imagen']); ?>"> </td> </tr> <?php } ?> </tbody> </table> </div> </div> </div> <!-- Optional JavaScript --> <!-- jQuery first, then Popper.js, then Bootstrap JS --> <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script> </body> </html>
🔗 Mysql Blob: https://dev.mysql.com/doc/refman/8.0/en/blob.html
🔗 MariaDB Blob: https://mariadb.com/kb/en/blob-and-text-data-types/
🎦 Esta lista de reproducción: https://www.youtube.com/playlist?list=PLCTD_CpMeEKS2Dvb-WNrAuDAXObB8GzJ0
Codigos en gdrive: https://drive.google.com/file/d/1tQwYvfL2jiUFc6beTWkOkGmXkq5zzFw2/view?usp=sharing
Gracias por apoyar este canal: https://www.patreon.com/programadornovato?fan_landing=true
🔗 Facebook: https://facebook.com/ProgramadorNovatoOficial
🔗 Twitter: https://twitter.com/programadornova
🔗 Linkedin: https://www.linkedin.com/in/programadornovato/
🔗 Instagram: https://www.instagram.com/programadornovato/
Ave que vuela, a la cazuela.