On 28.02.2001 04:57:04 ?iso-8859-1?q?Geetha=20Narayanan?= wrote:

>         can i able to store images in database, not in

Yes sure, see below.

> binary format but directly as image and retrieve

An image is nothing but binary data, so I suppose you're just confused.

Okay, here's how I do that.  I "only" use PHP, so that's all I know - things
will work differently in other languages, but it should still help you to get
the idea about how I'm doing things!

I have a table that has these fields:

     ID = auto_increment INT
     Type = ENUM('image/png', 'image/tif' ... )
     Data = MEDIUMTEXT

ID is the primary key.

In PHP I read the image file contents into a variable.  Then I base64 encode
it ( with base64_encode() ), find out what type of image this is (PNG, JPEG,
GIF....) and store both these data in a database table.

In code, it looks like this:

     $fp = fopen( $HTTP_POST_FILES['imgfile']['tmp_name'], "r" );
     $img_bin = fread( $fp, filesize( $HTTP_POST_FILES['imgfile']['tmp_name'] )
);
     fclose( $fp );

     $img_b64 = base64_encode( $img_bin );
     $img_type = $HTTP_POST_FILES['imgfile']['type'] );

     $q = 'INSERT INTO Images (Type, Data) VALUES ("' . $img_type . '", "' .
$img_b64 . '")';
     mysql_query( $q );

When I want to display the image, I read the row from the database, decode the
binary data
with base64_decode() and get the image type I stored (PNG, JPEG, GIF....).

This looks like this:

     $q = "SELECT Type, Data FROM Images WHERE ID = 42";
     $rs = mysql_query( $q );
     $row = mysql_fetch_object( $rs );

     $img_b64 = $row->Data;
     $img_bin = base64_decode( $img_64 );
     $img_length = strlen( $img_bin );
     $img_type = $row->Type;

Next I print to stdout the header, telling the browser what kind of image is
going to come ( header( "Content-Type: image/png" ); (or what not) ), like so:

     header( "Content-Type: " . $img_type );
     header( "Content-Length: " . $img_length );

After this I simply echo the file contents, so that the browser gets the data:

     echo $img_bin;

That's it!

Why am I doing this the way I am doing it?

I do not store the data in binary format in the database, so that the data
keep easier managable, because base64 encoding guarantees me, that I'll only
have "nice" ASCII characters in my data stream.  Dumping the data, or
managing it with phpMyAdmin becomes a lot easier when there's nothing but
ASCII data in my experience.  Further, if true binary data would be stored
in the table, you'd have to escape some "characters" in the binary stream,
like \0 (null character) ' or " and what not all else.  This will become a
hassle which I avoid by base64 encoding the data.

I also have to store the image type (PNG, JPEG, GIF...), so that it is
completely clear what kind of image I'm storing.  I don't know if the
browser would display the image correctly, if I don't send the correct
header when printing the image.

Cheers,
Alexander Skwar



---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to