Re: How can i put the image file to the db ? (mssql)
From: Ekkehard Morgenstern (ekkehard.morgenstern_at_onlinehome.de)
Date: Thu, 13 Nov 2003 23:45:02 +0100
Hi Alper Adatoz,
"Alper Adatoz" <firstname.lastname@example.org> schrieb im Newsbeitrag
> i just want to put jpeg file to the image field at the mssql db.
I've done this only with MySQL before, but I figure it could be similar with
1. You can create a BLOB field of the appropriate size in the database
2. Then, you can load the image file that has been uploaded by the user
(like, with file_get_contents() ).
3. Then, you encode the binary data to base64 format using base64_encode().
4. Then, you can simply use it in an INSERT or UPDATE SQL command:
$bindata = file_get_contents( $filename );
$b64data = base64_encode( $bindata );
$result = mysql_query( "INSERT INTO MYTBL ... SET IMAGE='$b64data'",
To retrieve the image stored in the database, simply read the row, access
the field, base64_decode() it and output it to the user.
For output to the user, you can use a specific PHP script that reads the
image from the database, sets the HTTP headers and then outputs the binary
See RFC 2616 (HTTP 1.1 specification), available from
http://www.rfc-editor.org, for information about the HTTP protocol.
This method has one disadvantage, that if the base64-encoded image data
would exceed the SQL INSERT/UPDATE command length limit. I haven't tried yet
if databases like MySQL can use arbitary-sized SQL queries.
I hope that helps.