save image to database

hi all im using php and mysql i was wondering if anyone could give me an example of:-

  1. the database
  2. upload image to database
  3. display image on php based webpage
    i need it to be done like this and not just a location stored to redirect to the image.
    any help great fully received

may not be the best way but when I display my images from a database I just update my table with the URL of the picture to display and display the row as an

might be the noobler way but never failed me.

cheers for the response but the image needs to be in the database and not a redirection the reason being its not just images its also documents etc but only a thumbnail is being displayed (which is a redirection) but the actual one that is to be downloaded needs to be in the database to stop people accessing it

It’s not possible to actually save objects and files in the database, a database is only there to store data and information (at least to my knowledge it is). What I would do is what was said earlier. In the table have a row that contains the file path, and also the path to the image file. Now in your html just create link with displaying the image from your database path for the image, then use the path of the file for the download.

When the user clicks on the link, it will point to where the file is and download said file, where ‘document’ is the path to the file and ‘thumbnail’ is the path to the thumbnail image:

<a href="downloads/<?php echo; $row_rsDetails['document']; ?>" > <img src="images/<?php echo $row_rsDetails['thumbnail']; ?>" ></a>

The above code is just a normal image link to a file to download, like a word document and the thumbnail is the image file reference in the table as well.

The only way to ‘stop’ people from accessing something from a page is to create a login system for your php so only certain users have access to certain information from your page and your database.

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…

Ah! I stand corrected. ;D

Kajih, you were partly correct. But, I have loaded pictures into a database because I wanted to be able to pulled them onto pages with very little code and to do it dynamically. But, it is much slower than using them as files. Basically, you can store ANYTHING in a database. That is how people store digital data. But, it really depends on the use of the data to tell you if it is good to use a database for that storage. Something like a video would never be put into a database, it would slow it up too much to be useful. And, after a few years of using pictures, it is best to store the file in a folder and use the database to point at it. That makes for only a little data, the path and filename, to be stored in the database. That small amount of text can be handled very quickly by databases. Anyway, I always seem to give more info than I started to…
CYA in the bitstream…

Sponsor our Newsletter | Privacy Policy | Terms of Service