ok. I have searched and searched. I need help getting a picture uploaded and stored in a mysql db and then query it and display the pic as a jpeg in a browser. I have been able to upload and run through everything but when I try and display the jpeg again from the db it comes out with binary garbage. I will include my scripts below. I got this script off of phpbuilder code library. I have found some syntax errors and corrected them but I can't get the jpeg to come out on a browser correctly. Please help!!!!!!! I think its the way that the picture is being uploaded to mysql and then dumped back down but I can't get my finger on it. The db field is a mediumblob and the field does report the correct size for the image when I upload it. Could it be something to do with "\" escapes?
--------------comm.inc -----------------------------------
<?
function connectdb(){
mysql_connect("localhost","user","password") or die(mysql_error());
mysql_select_db("db") or die(mysql_error());
}
?>
---------------------index.php------------------------------
<?
if (!isset($submit)) {
?>
<?
// tests to see if addslashes() is needed. 0 is needed 1 is not
echo ("addslashes status");
echo get_magic_quotes_gpc(); ?>
<form method="POST" action="" enctype=multipart/form-data>
<table>
<tr><td>Type</td><td><select name="imgtype"><option value="image/gif">GIF</option><option
value="image/jpeg">JPEG</option></select></td></tr>
<tr><td>File</td><td><input type="file" name="imgfile"></td></tr>
<tr><td></td><td><input type="submit" name="submit" value="upload"><input type="reset" value="reset"></td></tr>
</table>
</form>
<?
} else {
include "comm.inc";
connectdb();
$hndl=fopen($imgfile,"rb");
$imgdata='';
while(!feof($hndl)){
$imgdata.=fread($hndl, 2048);
}
$imgdata=addslashes($imgdata);
$sql = "INSERT INTO tblimage VALUES(NULL,'". $imgtype ."','".$imgdata."')";
@mysql_query($sql) or die(mysql_error());
fclose($hndl);
echo "<a href=\"view.php\">view image</a>";
};
?>
--------------post.php--------------------------
<?
include "comm.inc";
connectdb();
$sql = "SELECT * FROM tblimage WHERE imgid=". $imgid;
$result = @($sql) or exit("QUERY FAILED!");
$row=mysql_fetch_object($result);
$contenttype = @mysql_result($result,0,"imgtype");
$image = @mysql_result($result,0,"imgdata");
header("Content-type: $row->imgtype");
echo $row->imgdata;
?>
---------------view.php------------------------
<?
include "comm.inc";
connectdb();
$sql = "SELECT imgid,imgtype, imgdata FROM tblimage ORDER BY imgid";
$result = @($sql) or die(mysql_error());
echo "<table border=1>n";
echo "<tr><th>imgid</th><th>imgtype</th><th>imgdata</th></tr>n";
while ($rs=mysql_fetch_array($result)) {
echo "<tr><td>".$rs[0]."</td>";
echo "<td>".$rs[1]."</td>";
$stripped = stripslashes($rs[2]);
echo "<td><img src=\"post.php?imgid=".$stripped."\" width=100 height=100></td></tr>n";
};
echo "</table>n";
?>