Solved

upload image in coldfusion and cfloop

Posted on 2009-05-05
2
664 Views
Last Modified: 2013-12-24
Hello experts.
I have a code for upload -update-delete and resize image.
I named the image and file and queries with indicator 1.
I was to use this code for 8 image fields so i need this code changed with cfloop but i have problems.
Any help?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-7" />
<title>Untitled Document</title>
<cfset errors = "">
 <!--- Upload helper --->
 <cfparam name="Url.Art_ID" default="350">
<cfparam name="getimage1.img_Text" default="">
<cfparam name="getDBimage1.img_Text" default="">
<cfquery name="getDBimage1" datasource="#request.dsn#">
            SELECT img_Text
            FROM dbo.All_Images
            WHERE img_ID = <cfqueryparam value="1" cfsqltype="cf_sql_numeric">
            AND Artimages_ID = <cfqueryparam value="#URL.Art_ID#" cfsqltype="cf_sql_numeric">
            </cfquery>
 
<cfparam name="SERVERFILE1" default="">
 
   <cfset thumbWidth = 80>
   <cfset thumbHeight = 60>
 
   <cfset newWidth = 300>
   <cfset newHeight = 225>
   
   <cfset bigWidth = 640>
   <cfset bigHeight = 480>
   <!--- expandpath() gets the current
         drive and path of the file we are running --->
<CFSET thispath = expandpath("./images")>
<cfif isdefined ("uploadf1") AND CGI.REQUEST_METHOD IS "POST">
    <cfif structKeyExists(form,"foto1") and len(form.foto1)>
                
  <!--- uploads a file to our website --->
  
     <CFFILE action="upload" filefield="foto1" destination="#thispath#" nameconflict="MAKEUNIQUE" >
  <!--- check if is an image --->
        <cfif Lcase(file.ClientFileExt)NEQ "gif"
	      AND Lcase(file.ClientFileExt) NEQ "jpg"
          AND Lcase(file.ClientFileExt) NEQ "jpeg">
         <cffile  action="delete" file="#thispath#/#cffile.serverfile#">
         <cfset   errors = errors & "Image 1 is not an image file!<br />">
     
     <!--- check the image size --->
        <cfelseif file.Filesize GT 500 * 1024 >
          <cffile  action="delete" file="#thispath#/#cffile.serverfile#">
          <cfset errors = errors & "Image 1 is bigger than alowed!<br />">
        <cfelse>
    
     <!--- check if there is allready img_1 then delete the file to replace --->
          <cfif getDBimage1.Recordcount NEQ 0>
           <cffile action="delete" file="#thispath#/#getDBimage1.img_Text#">
           <cffile action="delete" file="#thispath#/thumbs/#getDBimage1.img_Text#">
           <cffile action="delete" file="#thispath#/bigsize/#getDBimage1.img_Text#">
          </cfif>
     <!--- end --->
     <!--- resize the file --->
             <cfimage name="uploadedImage"
             source="#thispath#/#cffile.serverFile#" >
             <cfimage action="resize"
             height="#newHeight#"
             width="#newWidth#"
             source="#uploadedImage#"
             destination="#thispath#/#cffile.serverFile#"
             overwrite="true"/> 
     
             <cfimage action="resize"
             height="#thumbHeight#"
             width="#thumbWidth#"
             source="#uploadedImage#"
             destination="#thispath#/thumbs/#cffile.serverFile#"
             overwrite="true"/>
    
             <cfimage action="resize"
             height="#bigHeight#"
             width="#bigWidth#"
             source="#uploadedImage#"
             destination="#thispath#/bigsize/#cffile.serverFile#"
             overwrite="true"/>
             <!--- <cfset ImgDir = expandpath("gallery/images/")><!--- full-size images dir --->
 
             <cfif NOT DirectoryExists(ImgDir)>
              
           <cfdirectory action = "create" directory = "#ImgDir#" name = "query name" mode = "777">
 
        </cfif> --->
 
    <!--- end --->
          <CFSET ServerFile1 = CFFILE.SERVERFILE>
   <!--- If there is allready img1 in DB then replace it --->
          <cfif getDBimage1.Recordcount NEQ 0>
             <cfquery name="updateimg_1" datasource="#request.dsn#">
             Update dbo.All_images
             SET img_text = '#ServerFile1#'
             WHERE Artimages_ID = <cfqueryparam value="#URL.Art_ID#" cfsqltype="cf_sql_numeric"> 
             AND  img_ID = <cfqueryparam value="1" cfsqltype="cf_sql_numeric">
             </cfquery>
   <!--- end --->
         <cfelse>
   <!--- If there is isn't img1 in Db then insert ---> 
            <cfquery name="imageupload" datasource="#request.dsn#">
            Insert into All_images (Artimages_ID,img_ID,img_text)
            values (#Url.art_id#, 1,'#ServerFile1#')
            </cfquery>
<!--- end --->
        </cfif>
     </cfif>
  </cfif>
</cfif>
<cfif isdefined ("Deletimg_1")>
<!--- Delete the img1 --->
    <cffile action="delete" file="#thispath#/#getDBimage1.img_Text#">
    <cffile action="delete" file="#thispath#/thumbs/#getDBimage1.img_Text#">
    <cffile action="delete" file="#thispath#/bigsize/#getDBimage1.img_Text#">
   <!--- end --->
   <!--- Delete img1 in DB ---> 
    <cfquery name="DeleteImg1"  datasource="#request.dsn#">
    DELETE From All_images
    WHERE Artimages_ID = <cfqueryparam value="#URL.Art_ID#" cfsqltype="cf_sql_numeric"> 
    AND  img_ID = <cfqueryparam value="1" cfsqltype="cf_sql_numeric"> 
    </cfquery>
  </cfif>
  
  <!--- After all get the img_1 if it exists --->
<cfquery name="getimage1" datasource="#request.dsn#">
SELECT img_Text
FROM dbo.All_Images
WHERE img_ID = <cfqueryparam value="1" cfsqltype="cf_sql_numeric"> 
AND Artimages_ID = <cfqueryparam value="#URL.Art_ID#" cfsqltype="cf_sql_numeric"> 
</cfquery>
 
<cfif getimage1.img_Text EQ "">
 <cfset image1 = 'images_1.jpg'>
 <cfelse>
 <cfset image1 = getimage1.img_Text>
 </cfif>
<!--- end --->
 
</head>
 
<body>
<CFIF errors NEQ "">
<CFOUTPUT>
      
            <CFLOOP list="#errors#" index="the_error"  delimiters="|">
                  <li>#the_error#</li><br />
            </CFLOOP>
     
</CFOUTPUT>
</CFIF>
<br />
<form action="#CGI.SCRIPT_NAME#" method="post" enctype="multipart/form-data" name="WADAUpdateForm1" id="WADAUpdateForm1">
<table width="500">
  <tr>
    <td><input name="imageField1" type="image" class="imageFieldinsert" id="imageField" src="images/thumbs/<cfoutput>#image1#</cfoutput>" width="80" height="60" /><br />
    <input name="foto1" class="textfieldfoto"  id="foto1"type="file"  /><br />
    <input name="uploadf1" type="submit"  id="uploadf1"   value="upload" width="60"  height="30" />
    <input name="Deletimg_1" type="submit"  id="Deletimg_1" value="Delete"  <cfif getimage1.img_Text EQ "">disabled="disabled" </cfif> /></td>
  </tr>
</table>
</body>
</html>

Open in new window

0
Comment
Question by:Panos
[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
2 Comments
 
LVL 17

Accepted Solution

by:
anandkp earned 500 total points
ID: 24347008
I am not sure what you are trying to do ... but i'll give it a try ... & see if it helps you ...

your existing code is fine (i think)

<cfset numImages = 8>
<cfloop index="i" from="1" to="#numImages#">
<cfquery name="getDBimage#i#" datasource="#request.dsn#">
SELECT img_Text
FROM dbo.All_Images
WHERE img_ID = <cfqueryparam value="#i#" cfsqltype="cf_sql_numeric">
AND Artimages_ID = <cfqueryparam value="#URL.Art_ID#" cfsqltype="cf_sql_numeric">
</cfquery>
</cfloop>

>> But for the query outputs :

i think u need to loop all over again ...
<cfloop index="i" from="1" to="#numImages#">
     <cffile action="delete" file="#thispath#/#Evaluate('getDBimage#1#.img_Text')#"> <!--- should this be file upload instead of file delete ? --->
     <CFSET "ServerFile#i#" = CFFILE.SERVERFILE>
</cfloop>

similarly ... u'll need to use evaluate again in the subsequent code ....

>> The updatequery
             <cfquery datasource="#request.dsn#">
             Update dbo.All_images
             SET img_text = '#Evaluate("ServerFile#i#")#'
             WHERE Artimages_ID = <cfqueryparam value="350" cfsqltype="cf_sql_numeric">
             AND  img_ID = <cfqueryparam value="1" cfsqltype="cf_sql_numeric">
             </cfquery>


>> The insert query:
<cfquery name="imageupload" datasource="#request.dsn#">
            Insert into All_images (Artimages_ID,img_ID,img_text)
            values (#Url.art_id#, 1,'#Evaluate("ServerFile#i#")#')
</cfquery>

>> i need a help

does this help ?

PS: U could even close this Q's to get ur Q points back - which u can use for further Questions ...
0
 
LVL 2

Author Closing Comment

by:Panos
ID: 31577977
Hi anandkp
Never mind for the points.
Thankyou for your help
regards
Panos
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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
json_decode return null? 8 105
Two nodes for updates and forwarding 8 51
Firewall Speed Issue 6 78
WAMP server installation, getting error popups, DLLs missing 5 90
In our day to day coding, how many times have we come across a necessity to check whether a URL is a broken link or not? For those of you that answered countless and are using ColdFusion like myself, then this article is for you.  It will show yo…
Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

734 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