?
Solved

Coldfusion - Store uploaded filename in Mysql db

Posted on 2012-09-22
8
Medium Priority
?
435 Views
Last Modified: 2012-09-23
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>
0
Comment
Question by:Bang-O-Matic
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 39

Accepted Solution

by:
gdemaria earned 800 total points
ID: 38425194
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
 

Author Comment

by:Bang-O-Matic
ID: 38425234
Hi gdemaria, where would I add file.serverFile into the cfupdate?
0
 
LVL 14

Assisted Solution

by:RickEpnet
RickEpnet earned 1200 total points
ID: 38425242
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
LVL 14

Expert Comment

by:RickEpnet
ID: 38425244
<cfset form.Logo = mfile1.serverfile>

 <cfupdate dataSource = "hwa"
    tableName = "listings"
    formFields = "Logo">
0
 
LVL 52

Expert Comment

by:_agx_
ID: 38425259
> 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
 

Author Comment

by:Bang-O-Matic
ID: 38425272
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
 

Author Closing Comment

by:Bang-O-Matic
ID: 38425276
Thanks again for the help!
0
 
LVL 52

Expert Comment

by:_agx_
ID: 38426701
> <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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article  is about submitting  form through  ColdFusion.Ajax.submitForm to the action page and send a response back in JSON format which later can be decoded using ColdFusion.JSON.decode. By this way you can avoid the usual page refresh for subm…
Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question