tags:

views:

37

answers:

1

Hi

i need help that how to store the images in the mysql table and retrieval of stored image using php code...

sample code that helps me a lot..

regards ~Deepu~

+1  A: 

Create a table in database with Blob field and another varchar field for picture type (jpeg/gif/etc..), store the picture in there.

To store the picture do the following:

  1. Read the picture into variable. Either fread of file_get_contents
  2. Insert picture data and picture type into database

To retrieve picture back do regular select statement to get the picture data and file type. Set the header Content-type to appropriate file type and display the picture data.

For example:

HTML
<img src="getPicture.php?id=12345" />

PHP

<?php
$id = (int) $_GET['id'];
// Assume $db is out DAL that is already connected and can query database 
$img = $db->loadObject("SELECT pic_data, pic_type FROM picture WHERE id = $id LIMIT 1");

// We get the following
// $img->pic_type = 'image/jpeg'
// $img->pic_data = 'picture data'

//
//
// Make sure there is not output prior setting header
header("Content-type: $img->pic_type");
echo $img->pic_data;

Look at this page. There is code it will help you http://www.anyexample.com/programming/php/php_mysql_example__image_gallery_(blob_storage).xml

Alex
+1 Good explanation
Daniel Vandersluis