Improve company productivity with a Business Account.Sign Up

x
?
Solved

Upload file

Posted on 2002-06-09
8
Medium Priority
?
271 Views
Last Modified: 2013-12-24
I would like to add an upload an image feature to the existing form:

Name:
Email:
Photo:

I know how to write the form and database for this and process the form for name and email ... but I'm not sure how to add the upload an image.  I've been looking at my CFML book and it appears that this would have to be done using a seperate form.  But is there a way to do it all on one form ... processing all fields, including the file upload all at once?

Thanks,
Ansia
0
Comment
Question by:Ansia
  • 4
  • 4
8 Comments
 
LVL 1

Expert Comment

by:ndintenfass
ID: 7065478
You can do this all on one form.

You will most probably just store the name of the image file in your database.  Alternatively, you may want to store the "ContentType" and "ContentSubType" fields.

One thing to always remember is that when you are uploading files in a form you MUST have the ENCTYPE="multipart/form-data" attribute in the FORM tag.

Here is some code that should get you started:


----- THE FORM -------

<form action="YourActionPage" method="post" enctype="multipart/form-data">
Name:<br>
<input type="text" name="name">
<br><br>
Email:<br>
<input type="text" name="email">
<br><br>
Photo<br>
<input type="file" name="photo">
<br><br>
<input type="submit" value="Go!">


----- THE CODE DEALING WITH THE FILE ON FORM ACTION -------
[[NOTE: THIS CODE ASSUMES THE FILE IS BEING UPLOADED, but you can check to see if it has been by seeing if the form field containing the file is an empty string -- if it's an emptry string, there is no file being uploaded]]


<!--- I like to build a struct with the file info --->
<cfset FileInfo = StructNew()>
<!--- this is the tag that puts the uploaded file on the server --->
<!--- you put in an absolute path -- in this case, it is just putting it into the directory of the calling page --->
<cfset directoryToStorePhotos = expandpath("./")>
<cffile action="UPLOAD" filefield="photo" destination="#directoryToStorePhotos#" nameconflict="MAKEUNIQUE">
<!--- if the file was saved, build a structure containing information about it --->
<cfif File.FileWasSaved>
     <cfset FileInfo.Path = directoryToStorePhotos>
     <cfset FileInfo.Name = File.ServerFile>
     <cfset FileInfo.Label = File.ClientFile>
     <cfset FileInfo.Type = File.ContentType>
     <cfset FileInfo.sub_type = File.ContentSubType>
</cfif>          
<!--- make sure the file is a web image, if it is not, delete it and set an error --->    
<cfif File.ContentType is not "image" or (File.ContentSubType is not "gif" AND File.ContentSubType is not "jpeg" and File.ContentSubType is not "pjpeg")>
     <!--- you will have your own way to deal with an error condition here, probably --->
     <cfset request.error = 1>
     <cffile action="Delete" file="#expandpath("images/pieces/#FileInfo.Name#")#">
     <cfset message="You can only upload gifs or jpegs">
</cfif>          


AT THIS POINT YOU NOW HAVE THE IMAGE UPLOADED TO YOUR SERVER AND HAVE THE NAME AND PATH OF THAT FILE IN THE FileInfo struct -- you can use this to insert into your database.  Then, you can dynamically populate the IMG tag when you go to display the image.  Also, if you are also allowing the end-user to edit the entry, you will need to detect if they are uploading a new photo and delete the old photo (or, at least, that's a common way to deal with it).

Make sense?
0
 

Author Comment

by:Ansia
ID: 7066024
yes .. it does make sense ... now to give it a try.

Question: In my database ... how do I name the field for the image? #imagename# ?

On the page where I want to display the info, it should then display where I have the field name #imagename# ? Am I to make a seperate directory in my remote folder for the images?  I'm trying to figure out how to point to the images so it will display along with the rest of the info.

Ansia
0
 
LVL 1

Expert Comment

by:ndintenfass
ID: 7066027
You name the field whatever you like.  "imagename" would do just fine.

You can create the directory structure however you like, but it would probably be wise to create a separate sub-directory that holds the uploaded photos.

For instance, if you created a directory called "photos" you would then set the line in the code I showed above to:

<cfset directoryToStorePhotos = expandPath("photos/")>

(assuming the page with the form is in the root above the directory photos)

Then, on the page where you show the image you could do something like:

<img src="photos/#imagename#">

Does that help?
0
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

 

Author Comment

by:Ansia
ID: 7080452
yes, very much helps.
but I'm also wondering: how to I get a user to upload the image file to my remote directory?

I want the image to go to "http://www.mydomain.com/uploads"

I've hit a wall here.  I think it's because you need a login/password to ftp files to my remote directory.

0
 
LVL 1

Expert Comment

by:ndintenfass
ID: 7080459
On the server you will use the CFFILE tag to capture the uploaded file and write it to disk.

Assuming the file that contains the CFFILE tag resides in the web root at a URL something like:

http://www.yourdomain.com/upload.cfm

On the upload.cfm page you can use the expandPath() function to put the file into the "uploads" directory, which appears to sit under the web root.  It might look something like this:

<cfset directoryToStorePhotos = expandpath("uploads/")>

<cffile action="UPLOAD" filefield="photo" destination="#directoryToStorePhotos#" nameconflict="MAKEUNIQUE">

Make sense?
0
 

Author Comment

by:Ansia
ID: 7080470
I'm starting to get it ...

In my access database, I have this field named "aphoto" and the field type set as "OLE Object".  Is this correct?

Here's what I have so far with the upload.cfm page:

<CFIF IsDefined("form.aDescription")>
  <CFINSERT DATASOURCE="aartra11.data"
  TABLENAME="available" FORMFIELDS="aDate,aDescription,aPriceTrade,aContact,aPhone,aEmail,aphoto">
</CFIF>

<body>
<CFFILE
DESTINATION="http://www.mydomain.com/AHTBC/uploads/"
ACTION="UPLOAD"
NAMECONFLICT="OVERWRITE"
FILEFIELD="aphoto">
<CFOUTPUT>
File upload was successful
</CFOUTPUT>
</body>

can you help me write this page correctly?
this is a new thing for me and I really appreciate the help!

Thanks :)
0
 
LVL 1

Accepted Solution

by:
ndintenfass earned 800 total points
ID: 7080484
Actually, no.  You are not going to store the image in the database, only the name of the image (and path, if you choose).  Change the field in your database to a text field, and use it to just store the name of the file.

Check out the code I originally posted.  In that example, the form contains a file field called "photo".  Also, make sure to put the ENCTYPE attribute in your FORM tag.

You will not be able to use CFINSERT to put the file or the name of the file into your database.

You might do something like:

<cfif isDefined("form.aDescription")>
     <!--- I like to build a struct with the file info --->
     <cfset FileInfo = StructNew()>
     <!--- this is the tag that puts the uploaded file on the server --->
     <!--- you put in an absolute path -- in this case, it is just putting it into the directory of the calling
     page --->
     <cfset directoryToStorePhotos = expandpath("uploads/")>
     <cffile action="UPLOAD" filefield="aPhoto" destination="#directoryToStorePhotos#" nameconflict="OVERWRITE">
     <!--- if the file was saved, build a structure containing information about it --->
     <cfif File.FileWasSaved>
         <cfset FileInfo.Path = directoryToStorePhotos>
         <cfset FileInfo.Name = File.ServerFile>
         <cfset FileInfo.Label = File.ClientFile>
         <cfset FileInfo.Type = File.ContentType>
         <cfset FileInfo.sub_type = File.ContentSubType>
     </cfif>          
     <!--- make sure the file is a web image, if it is not, delete it and set an error --->    
     <cfif File.ContentType is not "image" or (File.ContentSubType is not "gif" AND File.ContentSubType is not "jpeg" and File.ContentSubType is not "pjpeg")>
         <!--- you will have your own way to deal with an error condition here, probably --->
         <cfset request.error = 1>
         <cffile action="Delete" file="#expandpath("images/pieces/#FileInfo.Name#")#">
         <cfset message="You can only upload gifs or jpegs">
     </cfif>          
     <!--- now insert the information into the database --->
     <cfquery name="insert" datasource="aartra11.data">
          INSERT INTO available
          (
               aDate,aDescription,aPriceTrade,aContact,aPhone,aEmail,aphoto
          )
          VALUES
          (
               #createODBCDateTime(form.aDate)#,'#form.aDescription#','#form.aPriceTrade#','#form.aContact#','#form.aPhone#','#form.aEmail#','#fileInfo.name#')
          )
         
     </cfquery>
</cfif>

If you don't want to use a CFQUERY and like to continue to use CFINSERT, you may be able to do this in place of the CFQUERY tag above:

<CFSET FORM.aPhoto = fileInfo.name>

 <CFINSERT DATASOURCE="aartra11.data"
 TABLENAME="available" FORMFIELDS="aDate,aDescription,aPriceTrade,aContact,aPhone,aEmail,aphoto">
0
 

Author Comment

by:Ansia
ID: 7080598
Okay ... here goes!

If I have any problems, I'll post it in another question.

THANK YOU SO MUCH for your help!!! :)

Ansia
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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.

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

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…
Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
Watch the video to learn how one can deal with PST file corruption issue with an outstanding Kernel for Outlook PST Repair Tool easily. Using this tool, non-technical users can swiftly perform the repair process to restore their essential data witho…
From store locators to asset tracking and route optimization, learn how leading companies are using Google Maps APIs throughout the customer journey to increase checkout conversions, boost user engagement, and optimize order fulfillment. Powered …

588 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