Skip to main content
June 11, 2011
Question

Loop not working

  • June 11, 2011
  • 1 reply
  • 509 views

i have this flash image uploader, it creates 3 versions of each image that I upload, a max, middle and thumb version. Filenames like <max_file1.jpg>, <middle_file1.jpg> etc. I need to rename these files with an ID that is the latest ID from db + 1. The idea is that I end up with

<imgid_max.jpg>

<imgid_middle.jpg>

<imgid_thumb.jpg>

<imgid_thumbsitemanager.jpg>

And this for each file uploaded.

What happens is that I only end up with 1 series, the other files 'disappear' of they overwrite eachother, difficult to know. Why is this loop not working??

form.filescount = number of files uploaded

<cfloop from="1" to="#form.filescount#" index="idx">

<cfset currentimgid="" />

<!--- Get current imgid --->
<cfquery name="getimgid" datasource="#request.dba#">
    SELECT T_items_itemid
    FROM T_items
    ORDER BY T_items_itemid DESC
</cfquery>

<!--- set new imgid as request.currentimgid --->
<cfif getimgid.Recordcount eq 0>
    <cfset newIMGid = 1 />
<cfelse>
    <cfoutput query="getimgid" maxrows=1>
    <cfset newIMGid= T_items_itemid + 1 />
    </cfoutput>
</cfif>

<cfset request.currentimgid = #newIMGid# />

    <cfset thisCurrentFileName = evaluate("MultiPowUploadFileName_" & idx) />
    <cfset realFiletoRenamemax = "max_" & thisCurrentFileName>
    <cfset realFiletoRenamemiddle = "middle_" & thisCurrentFileName />
    <cfset realFiletoRenamethumb = "thumb_" & thisCurrentFileName />
       
        <cfif FileExists('#request.site.imgupload#\#realFiletoRenamemax#')>
          <cffile action="rename"
            source="#request.site.imgupload#\#realFiletoRenamemax#"
            destination="#request.site.imgupload#\#request.currentimgid#_max.jpg">
        </cfif>
        <cfif FileExists('#request.site.imgupload#\#realFiletoRenamemiddle#')>
          <cffile action="rename"
            source="#request.site.imgupload#\#realFiletoRenamemiddle#"
            destination="#request.site.imgupload#\#request.currentimgid#_middle.jpg">
        </cfif>
        <cfif FileExists('#request.site.imgupload#\#realFiletoRenamethumb#')>
          <cffile action="rename"
            source="#request.site.imgupload#\#realFiletoRenamethumb#"
            destination="#request.site.imgupload#\#request.currentimgid#_thumb.jpg">
        </cfif>
        <cfif FileExists('#request.site.imgupload#\#request.currentimgid#_thumb.jpg')>
            <cffile action="copy"
                source="#request.site.imgupload#\#request.currentimgid#_thumb.jpg"
                destination="#request.site.imgupload#\#request.currentimgid#_thumbsitemanager.jpg" />
        </cfif>
</cfloop>

    This topic has been closed for replies.

    1 reply

    Inspiring
    June 11, 2011

    Your problem might have something to do with the lack of a where clause in your query.  That makes it run the same query each time.

    On that topic, it would be more efficient to go to the database only once, and then do Q of Q inside your loop.

    June 12, 2011

    I already found it, I was writing the newimagid to the database outside the loop so it came up each time with the same id and the files where overwritten.

    BKBK
    Community Expert
    Community Expert
    June 13, 2011

    bianca_homedev wrote:

    I already found it

    Swell! Then please mark thread as answered, and we're all happy.