I have four files:
main.php my html submit form, which submits an image and text with the image
storeinfo.php it sends all my data from the html form to the database it works, my image and text from the form are successfully submitted
image.php fetches the image from the database and has a header function to convert aimagetype into whatever format the image is png, jpeg ect.
show.php fetches all text posted with the images and displays all image with the text, however the images are not displayed instead I get a blank box when an image fails to display.
I can't find my error, I'm guessing it has something to do with the header function in image.php or when I try to display the image with the html img tag in show.php. The upload of the image (which is stored as blob) to the database is successful. Why aren't the images displaying?
Code in order by each page:
main.php the html form
<form enctype="multipart/form-data" action="storeinfo.php" method="POST"> <table border=0 align=center bgcolor=black width=100%> <tr><td colspan=2><h2> </h2></td></tr> </table> <table border=0 align=center bgcolor=grey> <tr><td colspan=2><h2>Animal Information</h2></td></tr> <tr> <td>Name</td><td><input type=text name="aname"></td> </tr> <tr> <td>Description</td><td><input type=text name="adetails"></td> </tr> <tr> <td>Photo</td><td><input type=file name="aphoto"></td> </tr> <tr> <td></td><td><input type=submit name="submit" value="Store Information"></td> </tr> </table> </form>
storeinfo.php
<?php $conn = mysql_connect("localhost","root",""); if(!$conn) { echo mysql_error(); } $db = mysql_select_db("imagestore",$conn); if(!$db) { echo mysql_error(); } $aname = $_POST['aname']; $adetails = $_POST['adetails']; $aphoto = addslashes (file_get_contents($_FILES['aphoto']['tmp_name'])); $image = getimagesize($_FILES['aphoto']['tmp_name']);//to know about image type etc $imgtype = $image['mime']; $q ="INSERT INTO animaldata VALUES('','$aname','$adetails','$aphoto','$imgtype')"; $r = mysql_query($q,$conn); if($r) { echo "Information stored successfully"; } else { echo mysql_error(); } ?>
image.php
<?php $conn = mysql_connect("localhost","root",""); if(!$conn) { echo mysql_error(); } $db = mysql_select_db("imagestore",$conn); if(!$db) { echo mysql_error(); } $id = $_GET['id']; $q = "SELECT aphoto,aphototype FROM animaldata where id='$id'"; $r = mysql_query("$q",$conn); if($r) { $row = mysql_fetch_array($r); $type = "Content-type: ".$row['aphototype']; header($type); echo $row['aphoto']; } else { echo mysql_error(); } ?>
show.php
<?php //show information $conn = mysql_connect("localhost","root",""); if(!$conn) { echo mysql_error(); } $db = mysql_select_db("imagestore",$conn); if(!$db) { echo mysql_error(); } $q = "SELECT * FROM animaldata"; $r = mysql_query("$q",$conn); if($r) { while($row=mysql_fetch_array($r)) { //header("Content-type: text/html"); echo "</br>"; echo $row['aname']; echo "</br>"; echo $row['adetails']; echo "</br>"; //$type = "Content-type: ".$row['aphototype']; //header($type); //$lastid = mysql_insert_id(); // $lastid = $lastid; //echo "Your image:<br /><img src=image.php?id=$lastid />"; echo "<img src=image.php?id=".$row['id']." width=300 height=100/>"; } } else { echo mysql_error(); } ?>