Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

array, list or even other solution

Guest
Jun 04, 2011 Jun 04, 2011

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.

2.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guide , Jun 06, 2011 Jun 06, 2011

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.

Translate
Guide ,
Jun 05, 2011 Jun 05, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 05, 2011 Jun 05, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 05, 2011 Jun 05, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 05, 2011 Jun 05, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 05, 2011 Jun 05, 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>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 05, 2011 Jun 05, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 05, 2011 Jun 05, 2011

It looks like you want to be using bracket notation here:

form[MultiPowUploadFileName_1]

(I'm presuming the keys you're after are in the form scope here).

http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec22c24-7fb2.html

--

Adam

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 05, 2011 Jun 05, 2011

I still don't understand.

If I do

<cfset ThisCurrentFileName = ["MultiPowUploadFileName_" & i] />
<cfdump var="#ThisCurrentFileName#">
Result is

array
1MultiPowUploadFileName_1

I have MultiPowUploadFileName from 1 to max 9 But MultiPowUploadFileName_1 has a value, a filename, how do I get that one?

If i set <cfset FiletoRename = #MultiPowUploadFileName_1# > I get an desert.jpg, so far so good but then

<cfset RealFiletoRename = "thumb_" & #ThisCurrentFileName# >

I get an error Complex object types cannot be converted to simple values.And I understand why but I don't know what is the right way to do this.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jun 05, 2011 Jun 05, 2011

The square brackets have different meanings depending on how you use them.

form["MultiPowUploadFileName_" & i]

Means "return me th value of the submitted field called MultiPowUploadFileName_1"

["MultiPowUploadFileName_" & i]

Means "create an array, containing the string "MultiPowUploadFileName_1". This is what you've accidentally done, hence your error.

You just need to prepend "form" to the start so you're referencing the submitted variable not an implicit array.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 05, 2011 Jun 05, 2011

Unluckily there is no form scope, the variables are posted from a flash

application and have no scope, they are just called multipowupload_0,

multipowupload_1 and each variable stands for a filename that has been

uploaded.

Thank you for thinking with me.

Bianca

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jun 05, 2011 Jun 05, 2011

All variables in CF have a scope, if as you say they've been "posted" (in the http sense of the word) then yes, they're in the FORM scope.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 05, 2011 Jun 05, 2011

Every variable is in a scope.  And if a variable comes from an external source, it will be in either the form scope or the URL scope (depending on whether it came to CF via a POST or a GET request).  Or, I guess, the cookie scope.

Do a dump of the FORM and URL scopes.  I bet Owain's right arm you'll see your variables in one or other of them.

--

Adam

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jun 05, 2011 Jun 05, 2011

Not cool Adam, that's my favourite arm.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 05, 2011 Jun 05, 2011

All right, all right.

Owain's left arm.

(I dunno where your sense of community is, mate)

--

Adam

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jun 05, 2011 Jun 05, 2011

But that's my second favourite!

/predictable

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 05, 2011 Jun 05, 2011

Bianca_homedev wrote:

I have MultiPowUploadFileName from 1 to max 9 But MultiPowUploadFileName_1 has a value, a filename, how do I get that one?

If i set <cfset FiletoRename = #MultiPowUploadFileName_1# > I get an desert.jpg, so far so good but then

<cfset RealFiletoRename = "thumb_" & #ThisCurrentFileName# >

I get an error Complex object types cannot be converted to simple values.And I understand why but I don't know what is the right way to do this.

If you're sure all 9 indices are defined, you could do the following. Here, realFiletoRename dynamically holds the current file name.


<cfloop from="1" to="9" index="idx">
<cfset thisCurrentFileName = evaluate("MultiPowUploadFileName_" & idx)>
<cfset realFiletoRename = "thumb_" & thisCurrentFileName>
</cfloop>

In fact, you might even decide to store each file name separately for later use. Use for example, an array, as follows:

<cfset realFiletoRename = arrayNew(1)>
<cfloop from="1" to="9" index="idx">
<cfset thisCurrentFileName = evaluate("MultiPowUploadFileName_" & idx)>
<cfset realFiletoRename[idx] = "thumb_" & thisCurrentFileName>
</cfloop>

Alternatively, you could make the logic even much simpler with cfswitch. Here the index can take any of the values 1,2,...,9.

<cfswitch expression="#index#">
    <cfcase value="1">
        <cfset thisCurrentFileName = MultiPowUploadFileName_1>
    </cfcase>
    <cfcase value="2">
        <cfset thisCurrentFileName = MultiPowUploadFileName_2>
    </cfcase>
    <cfcase value="3">
        <cfset thisCurrentFileName = MultiPowUploadFileName_3>
    </cfcase>
    ...
    ...
    etc., etc.
</cfswitch>

<cfset RealFiletoRename = "thumb_" & thisCurrentFileName >

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jun 06, 2011 Jun 06, 2011

...OR...

Just put in the word "form" which has seemingly been forgotten? If ever I find myself reaching for the evaluate() function, that's when I know I'm doing something shoddy.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 06, 2011 Jun 06, 2011

Hi, Your answer helped me a great way around. I have one weird problem

though. I use

Filescount is the number of files uploaded. (In case there are less then 9)

Now I upload 4 files, and the code only seems to work for file no 3, it

skips the rest. Do you have any ideas?

Bianca

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jun 06, 2011 Jun 06, 2011

Okay, well if you're uploading four files, then doing effectively this:

<cfloop from="1" to="#form.Filescount#" index="i">

  <process i >

</cfloop>

And only three are getting processed, then the only explanation is that the form.Filescount variable is wrong somehow. Chuck in a bit of debugging - dump the form scope at the top of the page - see what you have. Is the Filescount variable correct?

Then in your loop - put something out to the screen to show you what it's processing, see where the issue is. Should be a fairly simply one to sort if you look at your code logically.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 06, 2011 Jun 06, 2011

@Bianca

You could of course use <cfloop from="1" to="#filescount#" index="idx">, as Owain has pointed out. If you still can't get it to work, then it might help to show us just the piece of code that needs fixing.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 06, 2011 Jun 06, 2011

Hi,

Here's the code I use. Weird enough it only treats the last image that gets

uploaded and not the other ones. I checked the filescount variable and that

one works fine.

</cfloop

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 06, 2011 Jun 06, 2011

Your code hasn't appeared, but I suspect you're using one variable to store the dynamic filenames, like this:

<cfloop from="1" to="3" index="i">

<cfset filename = "something_" & i>

</cfloop>

This code overwrites the value something_1 with something_2, and then overwrites the value something_2 with something_3. The final value of filename is therefore something_3.

If that is indeed what's happening in your case, then you should use an array or a struct. For example

<cfset filename = structnew()>

<!--- <cfset filename = arrayNew(1)> --->

<cfloop from="1" to="3" index="i">

<cfset filename = "something_" & i>

</cfloop>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 06, 2011 Jun 06, 2011

Oooh, I though it was executing the code for each step, I am indeed using

I was thinking that like this it would rename file 1 to 3 one by one, am I

wrong?

Bianca

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 06, 2011 Jun 06, 2011

Bianca_homedev wrote:

I was thinking that like this it would rename file 1 to 3 one by one, am I

wrong?


It would. But then, after each such renaming, you would have to do any business that depends on the index within the loop, before the loop closes.

<cfloop index="i">

<!--- do all your index-related business here involving index i --->

</cfloop>

As I have said, store the values in an array or struct if you wish to use them after the loop.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources