I'm trying to use php and mysql to store images and display them back when requested. I'm new to php and mysql and have tried to search on this and have found many solutions, which I've tried and none seem to work. I know I'm just missing something small, but the image displays as binary information (lots of weird characters). If someone has seen this before, please let me know what I'm missing.
If it's more complex than that, here's my code (truncated for ease):
The form:
<form action="?submit=TRUE" method="post" name="info" enctype="multipart/form-data">
<table>
<tr>
<td class="add"><img src="user.gif"> Picture:</td>
<td class="add"><input type="file" name="pic" size="50" value=""><br /></td>
</tr>
</table>
</form>
(there's other fields and a submit button which I've left out)
My table is described as the following:
id mediumint(9) primary key auto_increment
pic merdiumblob
pic_type varchar(50)
pic_size int(11)
I save the pic in the db as:
$db = mysql_connect("localhost", "cheersan", "fruit") or die("Can't connect to server.");;
mysql_select_db("cheersan_xmb1", $db) or die("Can't select database.");
$data = addslashes(fread(fopen($pic, "r"), filesize($pic)));
$sql = "INSERT INTO guest (pic, pic_type, pic_size) VALUES
('$data', '$pic_type', '$pic_size')";
$result = mysql_query($sql);
To retrieve and display the pic, I use (after connecting to the db):
$result = mysql_query("SELECT * FROM guest ORDER BY id DESC")
or die("Can't query database.");
while ($i = mysql_fetch_row($result)) {
echo "<table>\n";
echo " <tr>\n";
echo " <td><img src=\"$i[7] . '.jpg'\"><br /></td>\n";
echo " </tr>\n";
echo "<table>\n";
}
It actually is in field 7, as I mentioned above, I left some of the fields out so as to not type in non-relevant information.
The picture ends up being hundreds of lines of characters. What am I missing???
Thanks,
Rob