Coldfusion - Store uploaded filename in Mysql db

I am trying to upload an image to a folder and then insert the file name into my db table. I am missing something here because I all Im getting inserted into my table is .tmp file path.
example:  \cfusion\runtime\work\Catalina\localhost\tmp\neotmp5224950062783641114.tmp

I need the #file.serverFile# name not the .tmp file, could someone have a look at my code and tell me how to fix it? As always any help is well appreciated!


------------------------------------------------------------------------------------------

<cfquery name="GetRecordtoUpdate" datasource = "hwa">
SELECT * FROM Listings
WHERE ListID = #ListID#
</cfquery>
      <cfoutput query="GetRecordtoUpdate">
       

<!--- set the full path to the images folder --->
<cfset mediapath = expandpath('../pics')>

<!--- set the desired image height ---->
<cfset thumbsize = 150>

<!--- set the desired image width --->
<cfset imagesize = 320>


<cfif structKeyExists(form,"Logo") and len(form.Logo)>
   <cffile action="upload"
   filefield="Logo"
   destination="#MediaPath#"
   nameconflict="overwrite">
   
   <!--- read the image ---->
   <cfimage name="uploadedImage"
   source="#MediaPath#/#file.serverFile#" >

   <!--- figure out which way to scale the image --->
   <cfif uploadedImage.width gt uploadedImage.height>
         <cfset thmb_percentage = (thumbsize / uploadedImage.width)>
         <cfset percentage = (imagesize / uploadedImage.width)>
      <cfelse>
         <cfset thmb_percentage = (thumbsize / uploadedImage.height)>
         <cfset percentage = (imagesize / uploadedImage.height)>
   </cfif>
   
   <!--- calculate the new thumbnail and image height/width --->
   <cfset thumbWidth = round(uploadedImage.width * thmb_percentage)>
   <cfset thumbHeight = round(uploadedImage.height * thmb_percentage)>

   <cfset newWidth = round(uploadedImage.width * percentage)>
   <cfset newHeight = round(uploadedImage.height * percentage)>

   <!--- see if we need to resize the image, maybe it is already smaller than our desired size --->
   <cfif uploadedImage.width gt imagesize>
         <cfimage action="resize"
       height="#newHeight#"
       width="#newWidth#"
       source="#uploadedImage#"
      destination="#MediaPath#/#file.serverFile#"
       overwrite="true"/>
   </cfif>

    <!--- create a thumbnail for the image --->
    <cfimage action="resize"
    height="#thumbHeight#"
    width="#thumbWidth#"
    source="#uploadedImage#"
    destination="#MediaPath#/thumbs/#file.serverFile#"
    overwrite="true"/>
   
   
   <cfupdate dataSource = "hwa"
    tableName = "listings"
    formFields = "Logo">
   
   <cfoutput>
      <img src="../pics/thumbs/#file.serverFile#" height="#thumbHeight#" width="#thumbWidth#" align="left" hspace="10"><br>

      Original Image: #uploadedImage.width#x#uploadedImage.height#<br>
      Resized Image: #newWidth#x#newHeight#<br>
      Thumbnail: #thumbWidth#x#thumbHeight#<br><br>
      <a href="../pics/#file.serverFile#">See Image</a><br>
     
   </cfoutput>
</cfif>  

<form action="" method="post" enctype="multipart/form-data">
   <label for="Logo">Choose Image: </label>
   <input type="file" name="Logo"><input type="hidden" name="ListID" value="#ListID#" />
   <input type="submit" value="Upload Image">
</form>

</cfoutput>
Bang-O-MaticAsked:
Who is Participating?
 
gdemariaConnect With a Mentor Commented:
If you want the original name of the file, as the it existed on the user's computer, then use file.clientFile.

file.serverFile is giving you the name as it appears on the server, it may be renamed after uploading
0
 
Bang-O-MaticAuthor Commented:
Hi gdemaria, where would I add file.serverFile into the cfupdate?
0
 
RickEpnetConnect With a Mentor Commented:
You need to change your cffile code to include result

<cffile action="upload"
   filefield="Logo"
   destination="#MediaPath#"
   nameconflict="overwrite"  result="mfile1">

In this case this should be your file name
mfile1.serverfile

Also why store 3 different sizes of the same image. Just use cfimage to display the size you want.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
RickEpnetCommented:
<cfset form.Logo = mfile1.serverfile>

 <cfupdate dataSource = "hwa"
    tableName = "listings"
    formFields = "Logo">
0
 
_agx_Commented:
> Also why store 3 different sizes of the same image. Just use
       > cfimage to display the size you want.

Image manipulation can be resource intensive. It's typically more efficient to do resizing once on upload, rather than on every page request.
0
 
Bang-O-MaticAuthor Commented:
As far as I can see I'm only storing the 320px image and a thumbnail. I'd rather just go ahead and process them once. Thanks for the help!  It's all working now.
I do have another question related to this but I will post as a new question.
0
 
Bang-O-MaticAuthor Commented:
Thanks again for the help!
0
 
_agx_Commented:
> <img src="../pics/thumbs/#file.serverFile#"

Glad everything's working. Small tip - the FILE scope was deprecated a ways back in preference to CFFILE. One of these days FILE may stop working. Better to stick with the CFFILE scope or the "result" attribute like in RickEpnet's example.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.