• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 985
  • Last Modified:

cffile action="upload" + save filename to database

I'm using Coldfusion 9. We want to allow our account holders to upload files to our server. That part I've got working fine.

What I need to do now is capture the file name so that I can store that name in the datasbase with all the other account details.

Below is code that I found in another forum and have modified for my use. Please let me know what I need to add to make this work, or feel free to suggest something else.

Thanks in advance!

File #1: upload_bio.cfm

<head><title>Specify File to Upload</title></head> 
<body> 
Locate and select a file to upload:
<form enctype="multipart/form-data" method="post" action="upload_bio_action.cfm">
<input type="file" name="fileUpload" /><br />
<input type="submit" value="Upload File" />
</form>
</body>

Open in new window


File #2: upload_bio_action.cfm

<cfif isDefined("fileUpload")>
  <cffile action="upload"
     fileField="fileUpload"
     destination="(destination deleted)"
     accept="application/msword, application/pdf, text/html, text/plain, application/vnd.openxmlformats-officedocument.wordprocessingml.document"
     nameconflict="makeunique"> 
     <p>Thank you.</p>
   <p>  <cfoutput> 
You uploaded #cffile.ClientFileName#.#cffile.ClientFileExt# 
            successfully to the server.</p>
</cfoutput> 
     <p>Please close this window to continue.</p> 
</cfif>

Open in new window

0
kokopelliama3
Asked:
kokopelliama3
  • 4
  • 3
  • 2
  • +2
2 Solutions
 
stu215Commented:
<cffile action="upload" filefield="form.FileToUpload"
destination="d:\home\your_domain.com\wwwroot\uploads\" nameConflict="makeUnique">

<cfset Uploaded_File_Name = CFFile.ClientFile>

<CFOUTPUT>File: [#Uploaded_File_Name#]</CFOUTPUT>


Heres a link to the different parameters you can access that pertain to it:
http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-738f.html
0
 
Gurpreet Singh RandhawaWeb DeveloperCommented:
if you need the name of the file you can do always like this

<cfif isDefined("fileUpload")>
  <cffile action="upload"
     fileField="fileUpload"
     destination="(destination deleted)"
     accept="application/msword, application/pdf, text/html, text/plain, application/vnd.openxmlformats-officedocument.wordprocessingml.document"
     nameconflict="makeunique">
<cfset form.filename = cffile.serverfile>
     <p>Thank you.</p>
<cfquery name="#request.dsn#" name="iname">
insert into pictures(images) values('#form.filename#')
</cfquery>
   <p>  <cfoutput>
You uploaded #cffile.ClientFileName#.#cffile.ClientFileExt#
            successfully to the server.</p>
</cfoutput>
     <p>Please close this window to continue.</p>
</cfif>
0
 
_agx_Commented:
> capture the file name so that I can store that name in the datasbase
         > #cffile.ClientFileName#.#cffile.ClientFileExt#

Small comment.  Don't use the "clientXXXX" values for that.  ClientFile is the "Name of the file uploaded from the client’s system."  Using nameConflict="makeUnique" means the file name may change when it's saved to the server. If you store the original "client" name you won't be able to find the file on your server.

I've never had a reason to use the clientXXX variables.  With uploads, all I care about is the file on my server. So I always use the "serverXXX" variables

                ie #CFFILE.serverFile#
0
Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

 
Gurpreet Singh RandhawaWeb DeveloperCommented:
you can change this line

You uploaded #cffile.ClientFileName#.#cffile.ClientFileExt#

to

You uploaded #cffile.ClientFileName#.#cffile.ClientFileExt#. The File Saved on server and in database as #cffile.serverfile# due to naming conflicts
0
 
_agx_Commented:
myselfrandhawa - what's the with echo, LOL? That's *exactly* what I said 10 minutes ago.
0
 
Gurpreet Singh RandhawaWeb DeveloperCommented:
when i was typing, i think tht was the timeframe when u added ur comments, i kept my window open for a lot of time so that came in between
0
 
_agx_Commented:
That happens a lot, lol. Guess it's time to start refreshing the page more frequently ;-)
0
 
kokopelliama3Author Commented:
Hello everyone! Thanks for the speedy response. I will try these suggestions when I return to work tomorrow.

I appreciate the help and the comments! Good stuff!
0
 
_agx_Commented:
You basically had the answer already ;-) You simply need to use the CFFILE.ServerFile (not clientFile) in your database query.
0
 
srikanthmadishettiCommented:
No points.

Yes what agx mentioned is correct use  CFFILE.ServerFile
0
 
kokopelliama3Author Commented:
Thanks!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 4
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now