domingo, 26 de marzo de 2017

Subir y mostar imagenes en PHP

Creación de una base de datos donde se almacenaran las imágenes.


Conexión con la base de datos.



Formulario para carga de imagen.



Proceso donde se válida si se guardo la imagen.


Documento HTML donde se nos mostraran las imagenes insertadas, estas se ordenaran dentro de una tabla.



Códigos fuente.

conexion.php

<?php

$conexion = new mysqli("localhost","root","","imagen");

if($conexion){
echo "Conexion exitosa";
}

else{

echo "Conexion no exitosa";
}


?>


index.php

<!DOCTYPE html>
<html>
<head>
<title> INDEX DE IMAGENES </title>
<style type="text/css">
*{
  margin:0px;
  padding:0px;
}
body{
  background: #2F4F4F;
  background-position:center;
}

form{
  background: #FF0000;
  width: 360px;
  border:1px solid #4e4d4d;
  border-radius: 3px;
  -moz-border-radious:3px;
  -webkit-borde-radious:3px;
  box-shadow:inset 0 0 10px #000;
  margin:100px auto;
}
form h1{
  text-align: center;
  color:#000000;
  font-weight: normal;
  font-size: 40pt;
  margin:30px 0px;
}

form input{
  width:280px;
  height: 35px;
  padding: 0px 10px;
  margin:10px 30px;
  color:#6d6d6d;
  text-align: center;
}

form button{
  width:135px;
  margin:20px 0px 30px 30px;
  height: 50px;
  }

  form button:hover{
    background:#3a3a3a;
  }


</style>
</head>
<body>
<form action="proceso_guardar.php" method="POST" enctype="multipart/form-data"><br><br>
<input type="text" REQUIRED name="nombre" placeholder="Nombre..." value=""/><br> <br>
<input type="file" REQUIRED name="imagen"/><br><br>
<input type="submit" value="Aceptar">
</body>
</html>

proceso_guardar.php

<?php
include("conexion.php");
$nombre= $_POST['nombre'];
$imagen= addslashes(file_get_contents($_FILES['imagen']['tmp_name']));
$query="INSERT INTO tabla_imagen(nombre,Imagen) VALUES('$nombre','$imagen')";
$resultado= $conexion->query($query);

if($resultado){

echo "Si se inserto";
}

else{

echo "No se inserto";
}

?>

mostrar.php

<!DOCTYPE html>
<html>
<head>
<title> MOSTRAR IMAGENES </title>
</head>
<body bgcolor= #B2DFDB >
<center>
<table border=2>
<thead>
<tr>
<th>Id</th>
<th>Nombre</th>
<th>Imagen</th>
</tr>
</thead>
<tbody>
<?php
include("conexion.php");
$query="SELECT * FROM tabla_imagen";
$resultado= $conexion->query($query);
while($row = $resultado->fetch_assoc()){
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['nombre']; ?></td>
<td><img  height=200 src="data:image/jpg;base64,<?php echo base64_encode($row['Imagen']);?>"/></td>
</tr>
<?php
}
?>
</tbody>
</table>
</center>
</body>
</html>

1 comentario:

  1. Conexion exitosa No se inserto
    query($query);

    if($resultado){/*el problemas es qui*/

    echo "Si se inserto";
    }

    else{

    echo "No se inserto";
    }

    ?>

    ResponderEliminar