Solved

How to upload a photo via web form?

Posted on 2009-04-14
6
426 Views
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?
0
Comment
Question by:jasch2244
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 19

Expert Comment

by:erikTsomik
ID: 24141318
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
 
LVL 16

Accepted Solution

by:
duncancumming earned 500 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">
</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
 
LVL 1

Author Comment

by:jasch2244
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?
0
Manage your data center from practically anywhere

The KN8164V features HD resolution of 1920 x 1200, FIPS 140-2 with level 1 security standards and virtual media transmissions at twice the speed. Built for reliability, the KN series provides local console and remote over IP access, ensuring 24/7 availability to all servers.

 
LVL 52

Expert Comment

by:_agx_
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(".")>
0
 
LVL 16

Expert Comment

by:duncancumming
ID: 24142324
I believe it has to be the full path.  You could use something like
destination="#ExpandPath('../folder/images/')#"
0
 
LVL 52

Expert Comment

by:_agx_
ID: 24142334
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Use System DSN 6 91
PHP in Apache server 20 104
Two wordpress questions 3 76
Adding multiple JVM environments to RedHat 6 7 69
In this short web based tutorial, I wanted to show users how they can still use the powers of FrontPage in conjunction with Expression Web 3.  Even though Microsoft eliminated the use of Web components, we can still use them with FrontPage and edit …
Objective of This Article In 1990’s, when I was a budding software professional, I had a lot of confusion about which stream or technology, I had to choose to build my career. In those days, I had lot of confusion like whether to choose System so…
The purpose of this video is to demonstrate how to insert an Iframe into 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 : Open Page or Post…
The purpose of this video is to demonstrate how to Test the speed of a WordPress Website. Site Speed is an important metric of a site’s health. Slow site speed can result in viewers leaving your site quickly and not seeing your content. This…

756 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