Skip to main content
June 5, 2011
Answered

array, list or even other solution

  • June 5, 2011
  • 3 replies
  • 2803 views

i find this really complicated. i got a flashpage that uploads images. In fact for each image selected, it uploads 3 different formats (thumb_, midle_, max_)

So I upload image1.jpg and I get thumb_image1.jpg, middle_image1jpg, and max_image1.jpg

I can upload unto 9 images at at time, they will all have these three formats.

After uploading data get posted to the last file where I need to rename files and write them to db

The data that get's posted has the format:

MPuploadFileName_0 = sea.jpg

MPuploadFileName_1 = sea1.jpg

MPuploadFileName_3 = sea2.jpg

etc.

Dont forget, each image has 3 formats. In fact the MPuploadFileName_0 is hte 'main' filename and the others are details of it.

Each file upload get's an ID from the DB (don't ask me why, I didn't invent the software just have to work with it:-)) So I get latest ID, add 1 and write for exemple 104256 to db

now system supposes there is a 104256_max.jp, 104256_middle.jpg, 104256_thumb.jpg

This was for the 'main image'

<cffile action="rename"
    source="#request.site.imgupload#\#MPuploadFileName_0#_max"
    destination="#request.site.imgupload#\#request.currentimgid#_max.jpg">
  <cffile action="rename"
    source="#request.site.imgupload#\middle_#MPuploadFileName_0#"
    destination="#request.site.imgupload#\#request.currentimgid#_middle.jpg">
  <cffile action="rename"
    source="#request.site.imgupload#\thumb_#MPuploadFileName_0#"
    destination="#request.site.imgupload#\#request.currentimgid#_thumb.jpg">

This works, that is already that

Now the more difficult, for the 'detail images' the system supposes they are called 104256_detail2_max.jpg, 104256_detail2_middle.jpg, 104256_detail2_max.jpg, 104256_detail3_max.jpg, 104256_detail3_middle.jpg, 104256_detail3_max.jpg, etc

The number of uploaded files is in a variable named "filecount"

So I have to turn MPuploadFileName_1 = sea1.jpg that has been uploaded as thumb_sea1.jpg, middle_sea1jpg, and max_sea1.jpg into

104256_detail2_max.jpg, 104256_detail2_middle.jpg, 104256_detail2_max.jpg

This is what I tried:

<cfloop from="1" to="#filesCount#" index="i">
  <cfset ThisCurrentFileName = ["MultiPowUploadFileName_" & i] />
     
    <cfset count = 2 />
        <cfset ThisFileNametoChange = "thumb_"& ThisCurrentFileName />
        <cfset ThisNewFileName = [request.currentimgid & "_detail" & teller & "_thumb.jpg"] />
        <cffile action="rename"
            source="#request.site.imgupload#\thumb_#ThisCurrentFileName#"
            destination="#request.site.imgupload#\#thisNewFileName#">
        <cfset ThisFileNametoChange = "middle_"& ThisCurrentFileName />
        <cfset ThisNewFileName = [request.currentimgid & "_detail" & teller & "_middle.jpg"] />
        <cffile action="rename"
            source="#request.site.imgupload#\#ThisFileNametoChange#"
            destination="#request.site.imgupload#\#thisNewFileName#">
           <cfset ThisFileNametoChange = ["max_" & ThisCurrentFileName ] />
        <cfset thisNewFileName = [request.currentimgid & "_detail" & teller & "_max.jpg"] />   
        <cffile action="rename"
            source="#request.site.imgupload#\middle_#ThisCurrentFileName#"
            destination="#request.site.imgupload#\#thisNewFileName#">
      <cfset count = #count# + 1 />
</cfloop> 

ERROR;

Complex object types cannot be converted to simple values.

58 :           <cfset ThisFileNametoChange = "thumb_"& ThisCurrentFileName />

I hope i explained it well, any help would be greatly appreciated.

    This topic has been closed for replies.
    Correct answer Owainnorth

    I thought I was almost there but now it get's even more weird, here's the code that is supposed to rename the images 1 by 1 to a number I get from database into "request.currentimgid". But what is happening, I upload 8 images, I verify all images are uploaded and the cfdump of the form gives me the name of each image. "count" is correct.

    Then the following code runs and I end up with....... 3 files,

    487700_detail8_max.jpg

    487700_detail8_middle.jpg

    487700_detail8_thumb.jpg

    So the last uploaded file has been renamed correctly and the others..... gone away. I assure you, I checked they where in the directory before running this code, I checked with a cfdirectory and physically on the server.

    <cfloop from="1" to="#count#" index="idx">
         <cfset thisCurrentFileName = evaluate("MultiPowUploadFileName_" & idx) />
         <cfset realFiletoRenamethumb = "thumb_" & thisCurrentFileName />
         <cfset realFiletoRenamemiddle = "middle_" & thisCurrentFileName />
         <cfset realFiletoRenamemax = "max_" & thisCurrentFileName>
         <cfset thisNewfilenamethumb = request.currentimgid & "_detail" & count & "_thumb.jpg">
         <cfset thisNewfilenamemiddle = request.currentimgid & "_detail" & count & "_middle.jpg">
         <cfset thisNewfilenamemax = request.currentimgid & "_detail" & count & "_max.jpg">
        
         <cfif FileExists('#request.site.imgupload#\#realFiletoRenamethumb#')>
             <cffile action="rename"
                 source="#request.site.imgupload#\#realFiletoRenamethumb#"
                 destination="#request.site.imgupload#\#thisNewfilenamethumb#">
          </cfif>
          <cfif FileExists('#request.site.imgupload#\#realFiletoRenamemiddle#')>      
              <cffile action="rename"
                 source="#request.site.imgupload#\#realFiletoRenamemiddle#"
                 destination="#request.site.imgupload#\#thisNewfilenamemiddle#">
          </cfif>    
          <cfif FileExists('#request.site.imgupload#\#realFiletoRenamemax#')> 
               <cffile action="rename"
                 source="#request.site.imgupload#\#realFiletoRenamemax#"
                 destination="#request.site.imgupload#\#thisNewfilenamemax#">
           </cfif>
    </cfloop>


    Did you order the facepalm, sir? Spot the problem:

    <cfset thisNewfilenamethumb = request.currentimgid & "_detail" & count & "_thumb.jpg">
    <cfset thisNewfilenamemiddle = request.currentimgid & "_detail" & count & "_middle.jpg">
    <cfset thisNewfilenamemax = request.currentimgid & "_detail" & count & "_max.jpg">

    You're using the variable "count" rather than "idx". "Count" will always be 8, so it overwrites each previous file with the new one of the same name.

    3 replies

    June 5, 2011

    Hi again:-)

    I used what you said in a former post about arrays:

    <cfloop from="1" to="#filesCount#" index="i">
    <!--- Take advantage of CF's bracket notation to construct the form field name dynamically --->
      <cfset ThisCurrentFileName = ["MultiPowUploadFileName_" & i] />

    So for each "ThisCurrentFileName" there are three versions: _thumb, _middle and _max and I think that is where it is going wrong but I don't know how to do this. Should I use an array after all? Can you help me find the right way?

    Bianca

    BKBK
    Community Expert
    Community Expert
    June 5, 2011

    bianca_homedev wrote:

    Hi again:-)

    I used what you said in a former post about arrays:

    <cfloop from="1" to="#filesCount#" index="i">
    <!--- Take advantage of CF's bracket notation to construct the form field name dynamically --->
      <cfset ThisCurrentFileName = ["MultiPowUploadFileName_" & i] />

    So for each "ThisCurrentFileName" there are three versions: _thumb, _middle and _max and I think that is where it is going wrong but I don't know how to do this. Should I use an array after all? Can you help me find the right way?

    Bianca

    You could do like this:

    <cfloop from="1" to="#filesCount#" index="i">
      <cfset ThisCurrentFileName = "MultiPowUploadFileName_" & i />
    <!--- then use the names ThisCurrentFileName  dynamically within the loop --->
    ...
    ...
    </cfloop>

    or perhaps loop through the list of file types, like this

    <cfset nametypes = ""_thumb,_middle,_max">
    <cfloop list="#nametypes#"  index="type">
      <cfset ThisCurrentFileName = "MultiPowUploadFileName" & type />
    ...
    ...
    </cfloop>

    June 5, 2011

    If you set

    <cfset ThisCurrentFileName = "MultiPowUploadFileName_" & i />

    and do a cfdump then it says

    ThisCurrentFileName= MultiPowUploadFileName_1

    Then if I try to determine what filename should be renamed

    <cfset ThisFileNametoChange = "thumb_" &  ThisCurrentFileName />

    cfdump says

    ThisFileNametoChange =    thumb_MultiPowUploadFileName_1

    Of course this is not what I am looking for because in fact MultiPowUploadFileName_1= desert.jpg and MultiPowUploadFileName_2 = pinguin.jpg etc.

    Please, please help me out

    Bianca

    BKBK
    Community Expert
    Community Expert
    June 5, 2011
    <cfset ThisCurrentFileName = ["MultiPowUploadFileName_" & i] />

    That's the culprit. ColdFusion will read square brackets as an object.  You could do this of course:

    <cfset ThisCurrentFileName = "MultiPowUploadFileName_" & i >

    The same applies to all the other square brackets. Hence,  <cfset ThisNewFileName = request.currentimgid & "_detail" & teller & "_thumb.jpg"> in place of  <cfset ThisNewFileName = [request.currentimgid & "_detail" & teller & "_thumb.jpg"]>, and so on.

    June 5, 2011

    hi thanks for you answer. However though if I use

    <cfset ThisCurrentFileName = "MultiPowUploadFileName_" & i /> the dump says ThisCurrentFileName = MultiPowUploadFileName_1

    BUt MultiPowUploadFileName_1is a variable and has a value that I need to get

    If I use <cfset ThisCurrentFileName = MultiPowUploadFileName_ & i /> it says MultiPowUploadFileName_ is undefined

    which is true, how do I find a workaround? I feel I am so close but cannot seem to find the solution.

    Bianca

    Owainnorth
    Inspiring
    June 5, 2011
    <cfset ThisCurrentFileName = ["MultiPowUploadFileName_" & i] />Complex object types cannot be converted to simple values.

    58 :           <cfset ThisFileNametoChange = "thumb_"& ThisCurrentFileName />

    This error means that somewhere you're trying to use an array, object or struct as a number or string. It cannot be "ThisFileNameToChange" as that's the varible you're setting. It cannot be "thumb_" as that's a string literal, so it must be that the variable "ThisCurrentFileName" is not what you think it is.

    Dump it out to the screen, see what it is. I'd guess you've set it to be the entire form scope rather than one variable, or something like that.