I got the following code to upload the image
Code: Select all
<?php
// file_upload.php
// Check if the file uploaded successfully
if(is_uploaded_file($_FILES["image_file"]['tmp_name']))
{
$image_type = "";
//for security check the image type
if( $_FILES["image_file"]["type"] == "image/jpeg" || $_FILES["image_file"] ["type"] == "image/pjpeg" || $_FILES["image_file"]["type"] == "image/gif")
{
$image_type = $_FILES["image_file"]["type"];
$tamano = $_FILES["image_file"]['size'];
$image = addslashes (file_get_contents($_FILES['image_file']['tmp_name']));
echo($_FILES["image_file"]["type"] . "<br>");
echo("tamaño: " . $tamano ."<br>");
// here you can save the image and image type in DB
// You will output image type in header() when displaying image
$connection = mysql_connect("localhost:3306","root","toor")or
die ("Error en la conexión.");
$db = mysql_select_db("web",$connection)
or die ("Error en la selección de la bd.");
$query = "INSERT INTO imagenes (imagen, tipo,tamano) VALUES('$image', '$image_type', '$tamano')";
mysql_query($query) or die(mysql_error());
echo "<br>Image id is ".mysql_insert_id();
echo("<br><a href='prueba.php'>ver imagen</a><br>");
echo("<a href='default.htm'>insertar</a><br>");
}
else
{
echo("Invalid image type");
}
}
?>
html page
Code: Select all
html>
<head>
<title>Image Test</title>
</head>
<body>
<h1>Displaying image from database</h1>
<img src="verImagen.php?id=3" >
</body>
</html>Code: Select all
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$link = mysql_connect("localhost", "root", "toor") or die("Could not connect: " . mysql_error());
// select our database
mysql_select_db("web",$link) or die("use ".mysql_error());
//get image id
$idImagen = $_GET['id'];
// get the image from the db
$sql = "SELECT * FROM imagenes WHERE idImagen=" . $idImagen;
// the result of the query
$result = mysql_query("$sql") or die("Invalid query: " . mysql_error());
$arreglo = mysql_fetch_array($result);
// set the header for the image
//the type(tipo) and size(tamano) of the file are saved in the database
header('Content-type: ' . $arreglo['tipo'] );
header('Content-length: ' . $arreglo['tamano']);
//print image
echo $arreglo['imagen'];
// close the db link
mysql_close($link);
?>