mysql - Coldfusion output variable as image from database -
this function upload photo in map in our network. after sets path of photo , puts source in imagepath.
<cffunction name="uploadphoto" access="public" output="false" returntype="any"> <cfif isdefined("myform.submit")> <returnvariable="imagepath"> </cfif> <cfparam name="form.fileupload" default=""> <cfif len(trim(form.fileupload))> <cfset local.imagepath = 'img/profiles/' /> <cfset strpath = expandpath( "./" ) & local.imagepath /> <cffile action="upload" filefield="fileupload" destination="#strpath#" nameconflict="overwrite"> <p>thankyou, file has been uploaded.</p> </cfif> <cfset form.filename = cffile.serverfile> <cfreturn local.imagepath & form.filename/> </cffunction> function update's database , puts img source path (imagepath) in database. <cffunction name="savephoto" access="public" output="false" returntype="any"> <cfargument name="dbname" type="any" required="no" default="#server.cfc.core.getdbname()#" /> <cfargument name="myform" type="struct" required="yes"> <cfargument name="imagepath" type="any" required="yes"> <cfquery name="upload" datasource="#server.cfc.core.getdsn()#"> update logboek.tbl_users set photo=<cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.imagepath#"/> users_id=<cfqueryparam value="#session.login_logboek.user_id#" cfsqltype="cf_sql_integer"> </cfquery> </cffunction> <cffunction name="checkavatar" access="public" output="false" returntype="any"> <cfargument name="dbname" type="any" required="no" default="#server.cfc.core.getdbname()#" /> <cfquery name="checkavatar" datasource="#server.cfc.core.getdsn()#"> function checks if right user logged in see photo. select photo logboek.tbl_users users_id=<cfqueryparam value="#session.login_logboek.user_id#" cfsqltype="cf_sql_integer"> </cfquery> <cfif checkavatar.recordcount eq 1> <cfreturn checkavatar.photo /> </cfif> </cffunction> my problem lies in view want use image source path database output image on screen variables.checkavatar needs return "photo" database so:
<cfoutput> #variables.checkavatar# </cfoutput>
there several things going wrong here.
first , foremost, within function checkavatar, reassigning variable checkavatar query result. effectively, destroys function first time called. due cf's weird scoping rules, need first include line <cfset var checkavatar = -1> in function before query. further recommend not naming variable same thing function in.
your call function should end looking like
<img src="#checkavatar()#"/> since function returns path. may have additional problems sorting out difference between local file , web-accessible path file, hard debug remotely.
next, rid of <cfif checkavatar.recordcount eq 1> check, or modify handle case. if <cfreturn checkavatar.photo /> worst empty string if nothing found. currently, return nothing @ all, , crash page calls it. either throw explicitly or return default avatar image.
finally, use variables passed function arguments (as opposed passing in variables via session being done now). better design , easier reuse later.
Comments
Post a Comment