Solved

Upload file Coldfusion and inputing file path into DB

Posted on 2006-11-01
5
206 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 500 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This is a guide to setting up a new WHM/cPanel Server to be used for web hosting accounts. It is intended for web hosting company administrators and dedicated server owners. For under $99 per month (considering normal rate of Big Data Cetnters like …
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
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 Import and export files in WordPress. 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 : Click on Too…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now