Andy Jarrett // Code. Develop. Create.

Insert images into MySQL (or any DB really)

Never really had a need to do this, and as i type this i still cant think of a reason why to do this (im open to ideas). Either way it got asked on alt.comp.lang.coldfusion so i thought i'd find the solution and share it for my reference and anyone elses:
<cfoutput>
   <!--- The form to upload the image --->
   <form action="#cgi.SCRIPT_NAME#" method="post" enctype="multipart/form-data">
      <input type="File" name="imgFile"/>
      <input type="Submit" value="submit"/>
   </form>
   
   
   <cfif isDefined("form.imgFile")>
      <!--- UPload the image to the server --->
      <cffile
         action="UPLOAD"
         filefield="imgFile"
         destination="C:web"
         nameconflict="OVERWRITE"/>

      
      <!--- Read the file as binary to be inserted into the DB --->
      <cffile
         action="READBINARY"
         file="#cffile.SERVERDIRECTORY##cffile.SERVERFILE#"
         variable="binaryImgFile"/>

      
      <!--- Conver the binary to toBase64, this converts the binary to printable form --->
      <cfquery datasource="test" name="upldBinImg">
         INSERT INTO tablename
         SET q = '#toBase64(binaryImgFile)#'
      </cfquery>
   </cfif>
</cfoutput>

I’m here, learning and working away. If you liked this content and want to keep me going, consider buying me a coffee. Your support keeps this site running and the coffee brewing! ☕️