Putting a set of code which is pushing image to database and fetching back from database:
<!-- <?php
error_reporting(0);
// Connect to database
$errmsg = "";
if (! @mysql_connect("localhost","root","")) {
$errmsg = "Cannot connect to database";
}
@mysql_select_db("test");
$q = <<<CREATE
create table image (
pid int primary key not null auto_increment,
title text,
imgdata longblob,
friend text)
CREATE;
@mysql_query($q);
// Insert any new image into database
if (isset($_POST['submit'])) {
move_uploaded_file($_FILES['imagefile']['tmp_name'],"latest.img");
$instr = fopen("latest.img","rb");
$image = addslashes(fread($instr,filesize("latest.img")));
if (strlen($instr) < 149000) {
$image_query="insert into image (title, imgdata,friend) values (\"".
$_REQUEST['title'].
"\", \"".
$image.
"\",'".$_REQUEST['friend']."')";
mysql_query ($image_query) or die("query error");
} else {
$errmsg = "Too large!";
}
$resultbytes='';
// Find out about latest image
$query = "select * from image where pid=1";
$result = @mysql_query("$query");
$resultrow = @mysql_fetch_assoc($result);
$gotten = @mysql_query("select * from image order by pid desc limit 1");
if ($row = @mysql_fetch_assoc($gotten)) {
$title = htmlspecialchars($row[title]);
$bytes = $row[imgdata];
$resultbytes = $row[imgdata];
$friend=$row[friend];
} else {
$errmsg = "There is no image in the database yet";
$title = "no database image available";
// Put up a picture of our training centre
$instr = fopen("../wellimg/ctco.jpg","rb");
$bytes = fread($instr,filesize("../wellimg/ctco.jpg"));
}
if ($resultbytes!='') {
echo $resultbytes;
}
}
?>
<html>
<head>
<title>Upload an image to a database</title>
</head>
<body bgcolor="#FFFF66">
<form enctype="multipart/form-data" name="file_upload" method="post">
<center>
<div id="image" align="center">
<h2>Heres the latest picture</h2>
<font color=red><?php echo $errmsg; ?></font>
<b><?php echo $title ?></center>
</div>
<hr>
<h2>Please upload a new picture and title</h2>
<table align="center">
<tr>
<td>Select image to upload: </td>
<td><input type="file" name="imagefile"></td>
</tr>
<tr>
<td>Enter the title for picture: </td>
<td><input type="text" name="title"></td>
</tr>
<tr>
<td>Enter your friend's name:</td>
<td><input type="text" name="friend"></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="submit"></td>
<td></td>
</tr>
</table>
</form>
</body>
</html> -->
Above set of code has one problem. The problem is whenever i pressing the "submit" button. It is just displaying the image on a page. But it is leaving all the html codes. even any new line message after the
// Printing image on browser
echo $resultbytes;
//************************//
So, for this i put this set of code in html tag: This is other sample code:
<!--
<?php
error_reporting(0);
// Connect to database
$errmsg = "";
if (! @mysql_connect("localhost","root","")) {
$errmsg = "Cannot connect to database";
}
@mysql_select_db("test");
$q = <<<CREATE
create table image (
pid int primary key not null auto_increment,
title text,
imgdata longblob,
friend text)
CREATE;
@mysql_query($q);
// Insert any new image into database
if (isset($_POST['submit'])) {
move_uploaded_file($_FILES['imagefile']['tmp_name'],"latest.img");
$instr = fopen("latest.img","rb");
$image = addslashes(fread($instr,filesize("latest.img")));
if (strlen($instr) < 149000) {
$image_query="insert into image (title, imgdata,friend) values (\"".
$_REQUEST['title'].
"\", \"".
$image.
"\",'".$_REQUEST['friend']."')";
mysql_query ($image_query) or die("query error");
} else {
$errmsg = "Too large!";
}
$resultbytes='';
// Find out about latest image
$query = "select * from image where pid=1";
$result = @mysql_query("$query");
$resultrow = @mysql_fetch_assoc($result);
$gotten = @mysql_query("select * from image order by pid desc limit 1");
if ($row = @mysql_fetch_assoc($gotten)) {
$title = htmlspecialchars($row[title]);
$bytes = $row[imgdata];
$resultbytes = $row[imgdata];
$friend=$row[friend];
} else {
$errmsg = "There is no image in the database yet";
$title = "no database image available";
// Put up a picture of our training centre
$instr = fopen("../wellimg/ctco.jpg","rb");
$bytes = fread($instr,filesize("../wellimg/ctco.jpg"));
}
}
?>
<html>
<head>
<title>Upload an image to a database</title>
</head>
<body bgcolor="#FFFF66">
<form enctype="multipart/form-data" name="file_upload" method="post">
<center>
<div id="image" align="center">
<h2>Heres the latest picture</h2>
<?php
if ($resultbytes!='') {
// Printing image on browser
echo $resultbytes;
}
?>
<font color=red><?php echo $errmsg; ?></font>
<b><?php echo $title ?></center>
</div>
<hr>
<h2>Please upload a new picture and title</h2>
<table align="center">
<tr>
<td>Select image to upload: </td>
<td><input type="file" name="imagefile"></td>
</tr>
<tr>
<td>Enter the title for picture: </td>
<td><input type="text" name="title"></td>
</tr>
<tr>
<td>Enter your friend's name:</td>
<td><input type="text" name="friend"></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="submit"></td>
<td></td>
</tr>
</table>
</form>
</body>
</html>
-->
** But in this It is showing the image in format of special charaters and digits.
1) So, Please help me to print the image with some HTML code. So that i can print it in my form to display the image.
2) Is there any way to convert the database image into real image, so that i can store it into my hard-disk and call it from tag?
Please help me.