?
Solved

Upload file Coldfusion and inputing file path into DB

Posted on 2006-11-01
5
Medium Priority
?
215 Views
Last Modified: 2013-12-24
Hi experts!

I need some help with an upload of file in Coldfusion.

My goal is to create a system where a user will input some text and "attach" files ... send this text into a DB and send the path to the file uploaded into the DB.
so later on, I can query that DB and use that path to put up links on the website to those files.

I basically have a form:
<form action="Submit.cfm" enctype="multipart/form-data" method="post" name="myForm">
<textarea cols="100" name="text" rows="10"></textarea>
<input name="myFile1" type="file" />
<input name="myFile2" type="file" />
</form>

and in the submit page:
<cffile action="UPLOAD" nameconflict="overwrite" filefield="myFile1" destination="i:\websites\documents">
<cffile action="UPLOAD" nameconflict="overwrite" filefield="myFile2" destination="i:\websites\documents">
<cfquery name="insert" datasource="DB">
insert tablename
values('#text#','i:\websites\documents\#cffile.clientfile#')
</cfquery

I tried looking at the answer:
http://www.experts-exchange.com/Web/WebDevSoftware/ColdFusion/Q_21293970.html?query=get+filename+from+upload+file+ColdFusion&clearTAFilter=true

but it still not working for me...
I'm using CF mx

please help!
0
Comment
Question by:MetDia
  • 3
5 Comments
 
LVL 20

Expert Comment

by:trailblazzyr55
ID: 17852990
you're missing a few things in your code, and since you have two CFFILE's it'll get the data mixed up for the insert to you query...

try something like this...

<cfset myFileFormList = "myFile1,myFile2"> <!--- loop over the fields you want to insert/upload --->
<cfloop list="#myFileFormList#" index="myField">
      <cffile action="UPLOAD" nameconflict="overwrite" filefield="#myField#" destination="i:\websites\documents">
      <cfquery name="insert" datasource="DB">
      insert tablename (ColumnNameOne,ColumnNameTwo) <!--- don't forget to add the columns you want to insert to here --->
      values('#text#','i:\websites\documents\#cffile.clientfile#') <!--- then set the values you want to insert to those columns here --->
      </cfquery>
</cfloop>

now you have a unique CFFILE and you're query will work... you were also forgetting a closing bracket on your query that you posted...
0
 
LVL 20

Accepted Solution

by:
trailblazzyr55 earned 2000 total points
ID: 17853008
Sorry that was not correct, I forgot to add the correct insert syntax to what you had ;o)

forgot the insert INTO part, this should work better...

<cfset myFileFormList = "myFile1,myFile2"> <!--- loop over the fields you want to insert/upload --->
<cfloop list="#myFileFormList#" index="myField">
     <cffile action="UPLOAD" nameconflict="overwrite" filefield="#myField#" destination="i:\websites\documents">
     <cfquery name="insert" datasource="DB">
     insert into tablename (ColumnNameOne,ColumnNameTwo) <!--- don't forget to add the columns you want to insert to here --->
     values('#text#','i:\websites\documents\#cffile.clientfile#') <!--- then set the values you want to insert to those columns here --->
     </cfquery>
</cfloop>
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 17853099

>  insert tablename
>  values('#text#','i:\websites\documents\#cffile.clientfile#')

Seems you want to save to the table the location of the file on the server which would be #cffile.serverFile#
clientFile is the name of the original file from the PC; often they are the same.  

(( But you may want to end up changing the stored file name in the future if some of the file names are causing grief in your web browser.  For example, when you reference the file to download, if the file name contains characters that are not browser friendly it may be a challenge.  I usually store files in a "clean" name and also save the client file name in the table.  Then when downloading grab the file and give it to the user with the saved client file name.))


Also, I would not hard code the path in the insert, the #cffile.serverDirectory# variable will tell you where it ends up..

You have already told CF where to put the file in this command..

<cffile action="UPLOAD" nameconflict="overwrite" filefield="myFile1" destination="i:\websites\documents">

Try this...

  insert tablename
  values('#text#','#cffile.serverDirectory#\#cffile.serverFile#')

0
 
LVL 20

Expert Comment

by:trailblazzyr55
ID: 17853149
it'd still need the proper insert syntax

INSERT INTO [TABLENAME] ([Columns])
VALUES ([Column Values])
0
 

Author Comment

by:MetDia
ID: 17857583
got it!!!
thanks so much guys!
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
The purpose of this video is to demonstrate how to connect a WordPress website to Google Analytics. This will be demonstrated using a Windows 8 PC Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php :…
The purpose of this video is to demonstrate how to properly insert a Vimeo Video into a WordPress site or Blog. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp…
Suggested Courses

862 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