Re: How can i put the image file to the db ? (mssql)

From: Ekkehard Morgenstern (ekkehard.morgenstern_at_onlinehome.de)
Date: 11/13/03


Date: Thu, 13 Nov 2003 23:45:02 +0100

Hi Alper Adatoz,

"Alper Adatoz" <allper@myway.com> schrieb im Newsbeitrag
news:eae08b72.0311110522.62b12fd3@posting.google.com...
> 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
MSSQL.

1. You can create a BLOB field of the appropriate size in the database
table.
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'",
$dblink );

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
data.

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.

Regards,
Ekkehard Morgenstern.



Relevant Pages

  • Re: Can databases form automatic calculations?
    ... Basically I would like to know if there is some way to get a database ... All my product sales are ... Anyone know the MySQL function syntax for this if it is possible? ... The user interface is the SQL language which can be invoked ...
    (comp.databases)
  • DBConvert for MS SQL & MySQL 2.1.0
    ... DBConvert for MS SQL & MySQL is a database migration tool for data ... MySQL server directly. ...
    (comp.software.shareware.announce)
  • Re: Can databases form automatic calculations?
    ... Basically I would like to know if there is some way to get a database ... All my product sales are ... Anyone know the MySQL function syntax for this if it is possible? ... The user interface is the SQL language which can be invoked ...
    (comp.databases)
  • Re: Dabbling in plain-text databases
    ... plain-text databases (and if they're worth using in lieu of sql). ... will be sort of a video game fansite, that will have a news page (news ... parsing a file like that any faster or slower than using MySQL? ... An sql based database is better for situations requiring  better ...
    (comp.lang.php)
  • Re: Can databases form automatic calculations?
    ... Basically I would like to know if there is some way to get a database to ... by examining the relevant fields in the "Sales" table. ... Anyone know the MySQL function syntax for this if it is possible? ... The user interface is the SQL language which can be invoked directly from ...
    (comp.databases)