Link to home
Start Free TrialLog in
Avatar of jasocke2
jasocke2Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Uploading an image and inserting path into database problem...

Im currently building a website using coldfusion, I have nearly finished but having problems with the last bit.
What I am trying to do is get the user to upload a image from there computer.
What the code below does is inserts the image from the users computer on to the server in a designated folder (/students/jasocke2/uploadedimages/), and at the same time add the link to it in the database, (/students/jasocke2/uploadedimages/IMAGE_NAME.jpeg), so that if i was to select * from image it would display the image(s) that i require, BUT it doesn't quite work like that, it uploads the image from the users computer on the server fine but when it comes to inserting the link into the database it doesnt, in the image table there is an 'ImageID' field and a 'image1' field. It inserts the auto number in but there is no ul_path in the image1 field so it's obviously trying to inser it but it doesnt.
Any ideas on how it can be resolved?
The code is below..

<html>
<head>
<title>Upload</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<!--- first part --->
<cfif isdefined ("form.image1")>
<cfelse>
<form action="xx1.cfm" method="post" enctype="multipart/form-data" name="upload_form" id="upload_form">
<table width="450" border="1" align="center" cellpadding="5" cellspacing="0" bgcolor="#FFFFFF">
<tr bgcolor="#BACAE4">
<td>Upload An Image</td>
</tr>
<tr>
<td align="center"> </td>
</tr>
<tr>
<td align="center"><input type="file" name="Image1" id="Image1">
<input name="upload_now" type="submit" class="button" value="Upload the image"> </td>
</tr>
</table>
</form>
</cfif>
<!--- end of first part --->
<!--- 2nd part --->
<cfif isdefined("form.Image1")>
  <cffile action="upload" filefield="Image1" destination="#ExpandPath("/students/jasocke2/uploadedimages")#" nameconflict="overwrite" accept="image/*" >
  <cfset uploadedfile = "#ExpandPath("/students/jasocke2/uploadedimages/")#">
  <br>
  <br>
  <p align="center">Click to confirm this is the correct logo you wish to use</p>
  <table width="450" border="1" align="center" cellpadding="6" cellspacing="0">
    <tr valign="top">
      <td><cfoutput><img src="uploadedimages/#file.serverfile#"></cfoutput></td>
      <td><cfoutput>#file.ServerFile#</cfoutput> <cfoutput>
          <form action="xx2.cfm" method="post">
            <br>
            <br>
            <input type="submit" name="InsertIt" value="Click to confirm">
            <input name="Image" type="hidden" value="#file.ServerFile#">
          </form>
        </cfoutput> </td>
    </tr>
  </table>
</cfif>
<!--- end of second part --->
<!--- third part --->
<cfif isdefined ("form.InsertIt")>
<p align="center">You inserted the image name <cfoutput>#Form.image#</cfoutput> in to your database.<br>
Upload another image.</p>
<cfoutput>
<input name="Image" type="hidden" value="#form.image#">
</cfoutput>
<cfinsert datasource="jasvasquez-access"
tablename ="Image"
formfields="Image1">
</cfif>
<!--- end of third part --->
</body>
</html>

many thanks
James
Avatar of jasocke2
jasocke2
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Please note: Please ignore any <form action="xx1.cfm"> as they got abit mixed up while pasting in the code as I've been playing about with it putting it on different pages. Thanks
ASKER CERTIFIED SOLUTION
Avatar of pmascari
pmascari
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks, works a treat but in the database it inserts into image1
"/students/jasocke2/uploadedimages/C:\CFusionMX7\runtime\servers\coldfusion\SERVER-IN"
so it's inserting the start of the path but when it gets to the name of the picture it isnt inserting it so theres a problem with the "/#form.Image#')" bit, is it somthing to do with the #file.serverfile#.

i have edited out this bit of code from the code above...
"  <br>
  <br>
  <p align="center">Click to confirm this is the correct logo you wish to use</p>
  <table width="450" border="1" align="center" cellpadding="6" cellspacing="0">
    <tr valign="top">
      <td><cfoutput><img src="uploadedimages/#file.serverfile#"></cfoutput></td>
      <td><cfoutput>#file.ServerFile#</cfoutput> <cfoutput>
          <form action="TEST2.cfm" method="post">
            <br>
            <br>
            <input type="submit" name="InsertIt" value="Click to confirm">
            <input name="Image1" type="hidden" value="#file.ServerFile#">
          </form>
        </cfoutput> </td>
    </tr>
  </table>
"
as I dont want a page to confim what image they want to upload just a page where they select the image and a page where it inserts it into the database..
thanks
After the upload, is the image appearing on your page with the correct image name?  If you view the source code of that page, what is the value of the Image1 field?
all sorted thanks!!