How can I store an image in a database?

To store an image in a database we must first turn the image in a BLOB or Binary Large OBject.

Basically this is just the binary data of the image in one entity that can then be stored nice and easily in one field of a DB.

Luckily CF8 comes with a little function called imageGetBlob() - below is a little example of how it can be used...

First we have to use <cfimage> to create a ColdFusion image variable..

<cfimage action="read" 

We can then use this variable with imageGetBlob()

<cfset binimage = imageGetBlob(myImage)>

The binImage variable can now be inserted into a DB to keep it nice and safe!

N.B. This is a binary variable, so any fields used to contain this sort of data should have a suitable data type.

To display this image have a look at this article -

This question was written by Matt Evans
It was last updated on July 8, 2008.


Database / SQL


comments powered by Disqus