Solved

Cffile example please?

Posted on 2009-05-11
9
324 Views
Last Modified: 2013-12-24
My question is two part so I can get a better understanding of using cffile and how to impliment into my project. I'm trying to create a transaction center for a real estate application where the user can upload files to share with others (mortgage people, title / closing companies other agents etc.). I'm having a problem understanding how this will work using cffile (upload).
Question one: If the folder does not exsist on the server is there a way to create one via a form and coldfusion?
Question two: Do you have an example of how to use cffile to upload multiple documents at one time and store the name into the database?
0
Comment
Question by:jasch2244
  • 5
  • 2
  • 2
9 Comments
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 24360768

<!---

	If the form was submitted, process the files

--->

<cfif structKeyExists(form, "uploadButton")>

	<cfparam name="form.numberOfFiles" default="0">

	<!--- replace this with your folder path --->

	<cfset folderToStoreFiles = ExpandPath("myFiles")>

	<!--- create the directory if it does not exist --->

	<cfif not DirectoryExists(folderToStoreFiles)>

		<cfdirectory action="create" directory="#folderToStoreFiles#">

	</cfif>

	

	<!--- process each file ...--->

	<cfloop from="1" to="#form.numberOfFiles#" index="x">

		<!--- if a file was supplied, upload it ... --->

		<cfif structKeyExists(form, "uploadFile"& x) AND len(trim(form["uploadFile"& x]))>

			<!--- upload the current file to the desired directory --->

	 		<cffile action="upload"

				filefield="uploadFile#x#" 

    			destination="#folderToStoreFiles#" 

    			nameconflict="makeunique">

	

    		<!--- insert the file name into your database --->

			<cfquery name="insertFile" datasource="YourDatasourceName">

				INSERT INTO YourTableName ( FileNameColumn )

				VALUES 

				(

					<cfqueryparam value="#cffile.serverFileName#" cfsqltype="cf_sql_varchar">

				)

			</cfquery>
 

    		<!--- debugging: show uploaded file info --->

			<cfoutput>

				Processing file #x#: File uploaded to #cffile.serverDirectory#/#cffile.serverFileName#<br>

			</cfoutput>

		<cfelse>		

	    	<!--- debugging: show error --->

			<cfoutput>

				Processing file #x#: No file found. Skipping ...<br>

			</cfoutput>

		</cfif>

	</cfloop>

</cfif>	
 

<!---

	Create a form with however many fields you need

--->
 

<cfoutput>

<cfset numberOfFiles = 5>

<form method="post" enctype="multipart/form-data">

	<cfloop from="1" to="#numberOfFiles#" index="x">

		File #x# <input type="file" name="uploadFile#x#"><br>

	</cfloop>

	<input type="hidden" name="numberOfFiles" value="#numberOfFiles#">

	<input type="submit" name="uploadButton">

</form>

</cfoutput>				

Open in new window

0
 
LVL 52

Expert Comment

by:_agx_
ID: 24360789
Question one: If the folder does not exsist on the server is there a way to create one via a form and coldfusion?

Yes. You can use the cfdirectory tag to create folders on your server.
Question two: Do you have an example of how to use cffile to upload multiple documents at one time and store the name into the database?

See my example above.  It creates a form with 5 file fields with the names uploadFile1, uploadFile2, ...., uploadFile5.  (But you can create as few or as many as you want).  When the form is submitted, it uses a a loop to process each of the files:

- First it uses cffile to upload each file
- Uses cfquery to insert the file _name_ (ie someFile.jpg) into a database table.  Just replace the datasource, table and column names with your own.
0
 
LVL 27

Expert Comment

by:azadisaryev
ID: 24360858
adobe cf livedocs has all the examples you may need:
<cffile>: http://livedocs.adobe.com/coldfusion/8/Tags_f_02.html
<cffile action="upload">: http://livedocs.adobe.com/coldfusion/8/Tags_f_10.html

PS: you can download CFML Reference Manual and CF Dev Guide free PDFs from adobe.com if you do not have them: http://www.adobe.com/support/documentation/en/coldfusion/

as for your questions:

>> is there a way to create [a folder] via a form and coldfusion?

yes, there is. check out <cfdirectory action="create"> tag:

<cfdirectory>: http://livedocs.adobe.com/coldfusion/8/Tags_d-e_03.html
using <cfdirectory>: http://livedocs.adobe.com/coldfusion/8/manageFiles_7.html

>> example of how to use cffile to upload multiple documents at one time and store the name into the database?

read these:
http://www.bennadel.com/blog/1117-Ask-Ben-Uploading-Multiple-Files-Using-ColdFusion.htm
http://www.devarticles.com/c/a/ColdFusion/Multiple-File-Upload-with-CFFILE/
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/Cold_Fusion_Markup_Language/Q_23730630.html

Azadi
0
 
LVL 52

Expert Comment

by:_agx_
ID: 24360897
Yes, definitely get yourself a copy of livedocs or bookmark it in your browser.  Most likely you will use it on a daily basis.
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 27

Expert Comment

by:azadisaryev
ID: 24361126
SOT:

http://www.cfquickdocs.com/cf8/

same content (sans comments) as livedocs, and about 1000 times faster...
plus a FF search plug-in!
great for quick look-ups of tag/function syntax (though i personally tend to just have the CFML reference PDF open...)

Azadi
0
 
LVL 52

Expert Comment

by:_agx_
ID: 24361292
> plus a FF search plug-in!

Sweet! I did not know they had a plugin.
0
 
LVL 1

Author Comment

by:jasch2244
ID: 24369193
_agx_: I'm able to get your code to work (thank you exactly what I'm looking for). But I can't get the file name to be stored in the database with a file extension (example: myfile.pdf is stored as myfile) is there  a work around for this? I'm using mySQL
0
 
LVL 1

Author Comment

by:jasch2244
ID: 24369330
I think I found it.. in the portion of the code: <cfqueryparam value="#cffile.serverFileName#" cfsqltype="cf_sql_varchar"> I changed the .serverFileName to .serverFile and it worked! Is there a disadvantage to doing this?
0
 
LVL 52

Expert Comment

by:_agx_
ID: 24371041
> Is there a disadvantage to doing this?

jasch2244, sorry that was a typo. No there is no disadvantage. It is strictly a matter of using whichever field you need for your purposes.  In your case, yes, you want  the one with the file extension.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Website being blocked? 3 122
.htaccess rewrite url with querystring problem 13 86
Problem to go to Web page 2 97
exchange 2010 turning off 3des ciphers 2 118
Have you ever sent email via ColdFusion and thought of tracking this mail to capture the exact date and time when the message was opened ?  If yes, then this article is for you ! First we need a table user_email with columns user_id , email , sub…
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

910 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

21 Experts available now in Live!

Get 1:1 Help Now