Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 430
  • Last Modified:

How to upload a photo via web form?

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?
0
jasch2244
Asked:
jasch2244
1 Solution
 
erikTsomikSystem Architect, CF programmer Commented:
simply use <cffile > tag to do that
and read this
http://www.dmxzone.com/go?2831
 if you have any questions I will help you
0
 
duncancummingCommented:
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">
</form>

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 --->
 
	<cftry>
	<!--- always a good idea to wrap any cffile stuff in a cftry --->
		<cffile
			action="UPLOAD" 
			
			<!--- name of form field --->
			filefield="ImageFile" 
			
			<!--- image mime types you allow --->
			accept="image/png, image/jpg, image/bmp, image/gif, image/jpeg" 
			
			<!--- where to save the file --->
			destination="C:\yourFolder\" 
			
			<!--- if filename already exists, make a new name for it --->
			nameConflict="MAKEUNIQUE"> 
			
		<!--- now the file's been uploaded, save details to database --->
		<cfquery name="saveImage" datasource="dsn">
			INSERT
			INTO Images (Filename, Filesize, Filetype, DateCreated)
			VALUES 
			(
				'#CFFile.ServerFile#', 
				'#CFFile.FileSize#', 
				'#CFFile.ServerFileExt#'
				<cfqueryparam cfsqltype="CF_SQL_DATE" value="#CreateDate(Year(Now()), Month(Now()), Day(Now()))#">
			)
		</cfquery>
		
		<cfcatch type="Any">
		<!--- What to do if there's any errors? --->
			<cfdump var="#cfcatch#"><cfabort>
		</cfcatch>
	</cftry>
<cfelse>
<!--- tried to navigate straight to the action page? Maybe want to redirect --->
	<cflocation url="page1.cfm" addtoken="No">
</cfif>

Open in new window

0
 
jasch2244Author Commented:
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?
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
_agx_Commented:
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(".")>
0
 
duncancummingCommented:
I believe it has to be the full path.  You could use something like
destination="#ExpandPath('../folder/images/')#"
0
 
_agx_Commented:
You can also use relative paths:

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

ExpandPath Documentation:
http://livedocs.adobe.com/coldfusion/7/htmldocs/00000460.htm
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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now