YES! You CAN insert pictures into a database, although it is not suggested. (Pictures can be large and are slow to upload and download as database data. Therefore, it is MUCH MUCH better to store the picture inside a folder and place a link or filename in the database. In this manner, you can store a picture name or a path and name that points at the picture file. Then, load it using the
tag. In that manner it loads quickly without all the database calls and data shoveling… Okay, not that is said, here is some code… OH, inside the database set the picture data area as “BLOB”…
Picture Upload to Database:
(Assumes database connection made, table name: Picture, fields:PixFileName/PixType/PixTitleInput is file)
[php]
$handle = fopen(“testpic.jpg”, “rb”);
$img = fread($handle, filesize(‘testpic.jpg’));
fclose($handle);
$img = base64_encode($img);
$sql = “insert into pictures values(’$img’,‘jpg’,‘picture title’)”;
mysql_query($sql) or die(‘Bad Pix Query’);
echo “Success! You have inserted your picture!”;
?>
[/php]
To read it back, something like this…
[php]
$sql = “select pic, ext from pictures where title=‘picture title’”; //(This would depend on your DB set up!)
$result = mysql_query($sql) or die(‘Bad Pix query’’.mysql_error());
while($row = mysql_fetch_array($result,MYSQL_ASSOC)){
$db_img = $row[‘pic’];
$type = $row[‘ext’];
}
$db_img = base64_decode($db_img); //print_r($db_img );
$db_img = imagecreatefromstring($db_img);
if ($db_img !== false) {
switch ($type) {
case “jpg”:
header(“Content-Type: image/jpeg”);
imagejpeg($db_img);
break;
case “gif”:
header(“Content-Type: image/gif”);
imagegif($db_img);
break;
case “png”:
header(“Content-Type: image/png”);
imagepng($db_img);
break;
}
}
imagedestroy($db_img);
?>
[/php]
Note this is just code, it is NOT tested with your set up. You will have to alter it for your use.
It is just a sample of how to get the pix back from the database.
If you can not get it to work, show us how you stored the picture and your new code to display it…
Good luck…