How to upload a photo via web form?

Posted on 2009-04-14
Medium Priority
Last Modified: 2013-12-24
I'm looking for code or an example on how I can upload photos to a website via coldfusion script to a database or file that is linked from a database record (hope that makes sense). Basically, I need to upload a photo for a particular record. How would I go about doing this? Any Code examples or steps I can follow?
Question by:jasch2244
LVL 19

Expert Comment

ID: 24141318
simply use <cffile > tag to do that
and read this
 if you have any questions I will help you
LVL 16

Accepted Solution

duncancumming earned 2000 total points
ID: 24141441
Firstly, your form should look something like this (obviously with extra fields for whatever else you need to ask):

<form name="whatever" action="page2.cfm" method="POST" enctype="multipart/form-data">
    Image: <input type="File" name="ImageFile"><br>
    <input type="submit" name="Submit" value="Submit">

The important bit there is the enctype attribute on the form tag; no file upload will work without it.  The action should be either this page or another page that will do the work of saving the file and storing details in the database.

The contents of that file should look a bit like this.  This is a fairly simple example, you'd probably modify it to refer to your record that you want to attach the photo to.  After the image is uploaded, there are various values available in the CFFile scope.  

<cfparam name="Form.ImageFile" default="">
<!--- repeat for any extra fields in your form --->
<cfif IsDefined("Form.FieldNames")>
<!--- user has submitted the form --->
	<!--- always a good idea to wrap any cffile stuff in a cftry --->
			<!--- name of form field --->
			<!--- image mime types you allow --->
			accept="image/png, image/jpg, image/bmp, image/gif, image/jpeg" 
			<!--- where to save the file --->
			<!--- if filename already exists, make a new name for it --->
		<!--- now the file's been uploaded, save details to database --->
		<cfquery name="saveImage" datasource="dsn">
			INTO Images (Filename, Filesize, Filetype, DateCreated)
				<cfqueryparam cfsqltype="CF_SQL_DATE" value="#CreateDate(Year(Now()), Month(Now()), Day(Now()))#">
		<cfcatch type="Any">
		<!--- What to do if there's any errors? --->
			<cfdump var="#cfcatch#"><cfabort>
<!--- tried to navigate straight to the action page? Maybe want to redirect --->
	<cflocation url="page1.cfm" addtoken="No">

Open in new window


Author Comment

ID: 24142293
When using a shared windows environment (server) and using the destination (destination="C:\yourFolder\") attribute do yo have to use the full file path? could you not do /folder/imagesA/? Not sure how to point to the server with absolute path under my server settings. Any thoughts?
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

LVL 53

Expert Comment

ID: 24142314
Yes, you have to use an absolute path.  You can also get that by using ExpandPath(".").  That would give you your current path:

<cfset currentPath = ExpandPath(".")>
LVL 16

Expert Comment

ID: 24142324
I believe it has to be the full path.  You could use something like
LVL 53

Expert Comment

ID: 24142334
You can also use relative paths:

<!--- get the directory above the current one --->
<cfset parentDirect = ExpandPath("../")>

ExpandPath Documentation:

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
Lease-to-own eliminates the expenditure of hardware replacement and allows you to pay off the server over time. Usually, this is much cheaper than leasing servers. Think of lease-to-own as credit without interest.
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…
The purpose of this video is to demonstrate how to integrate Mailchimp with Facebook. This will be demonstrated using a Windows 8 PC. Mailchimp and Facebook will be used. Log into your Mailchimp account. : Click on your name. Go to Account Setti…
Suggested Courses

597 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