Topic: getting assigned key from $_FILES array (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=27968" title="Pages that link to Topic: getting assigned key from $_FILES array (Page 1 of 1)" rel="nofollow" >Topic: getting assigned key from $_FILES array <span class="small">(Page 1 of 1)</span>\

 
butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 05-21-2006 23:47

I have an upload form that allows the user to upload multiple files at once if they want to. I'm using name="photos[]" to have all the uploads in an array on submit. Without going into a huge explaination, I would like to number the keys in the form fields i.e

code:
name="photos[1]" 
name="photos[3]"  
name="photos[7]"



but I don't seem to be able to access the key after a submit. I want to do something like

code:
foreach ($_FILES['photos'] as $key => $value)
{
  echo $key.'<br />';
}



and have it echo
1
3
7

but no matter what I try I don't seem to be able to access the key assigned in the field name. I tried var_dump($_FILES) and print_r($_FILES['photos']) and print_r($_POST) and I don't see the assigned field keys anywhere.

Is this an impossible task?

<edit>
Had to put the code for the fields in UBB code tags or they got parsed for usernames.
</edit>

(Edited by butcher on 05-21-2006 23:51)

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 05-22-2006 01:37

I don't think you can put the keys in like that. In this case I don't think you're getting an array you're just getting three values

$_FILES['photosnt'] etc.



.:[ Never resist a perfect moment ]:.

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 05-22-2006 02:56

Thanks Bit

I did finally get it and although I'm almost positive I tried this at least once along the way this is what gave me the assigned keys.

code:
foreach ($_FILES['photos'] as $photoArray)
{
  foreach ($photoArray as $key => $value)
  {
    echo $key.'-'. $value;
  }
}


Seems so simple I can't believe it gave me so much trouble figuring it out.

- Butcher -

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 05-22-2006 23:24

Ah interesting.



.:[ Never resist a perfect moment ]:.

poi
Paranoid (IV) Inmate

From: Norway
Insane since: Jun 2002

posted posted 05-22-2006 23:30

AFAIR it also work like that for the other kind of form field.



(Edited by poi on 05-22-2006 23:31)

CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

posted posted 05-23-2006 03:51

let me ask a silly question. How are you naming the "picture" fields in the form itself? Just curious.

Later,

C:\

CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

posted posted 05-23-2006 04:23

never mind. I didn't know you could have the names of the fields as "field[]".

Later,

C:\

divinechaos
Nervous Wreck (II) Inmate

From:
Insane since: Dec 2001

posted posted 05-23-2006 04:58

Yep. It's a feature of PHP. If you add a foo[] or foo[bar] in either a POST variable (

code:
name="foo[]"

) or GET variable (

code:
?foo[]=bar

), PHP interprets it appropriately. It's not done by Apache or the browser -- this is a feature of the PHP interpreter itself.

Regards,
DC



Post Reply
 
Your User Name:
Your Password:
Login Options:
 
Your Text:
Loading...
Options:


« BackwardsOnwards »

Show Forum Drop Down Menu