Jump to content

Recommended Posts

I'm looping through an html form that uploads files. It works but in trying to make it more sophisticated by listening for certain input names.  Since $_FILES returns a 2d array i need to analyze the keys...any why doesn't the following code work?

 

//this resides inside an isset()
          reset($_FILES); //make sure array starts from beginning ??? not working
foreach($_FILES as $file){
	echo key($_FILES);
	next($_FILES);
	//doa bunch of uploading and re-naming stuff...
}
//returns all keys but #1 "thumb1" ???

 

the html...

 

<form name="uploadnew" enctype="multipart/form-data"  action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
    thumbnail 1 <input type="file" name="thumb1" /><br />
    thumbnail 2 <input type="file" name="thumb2" /><br />
    thumbnail 3 <input type="file" name="thumb3" /><br />
    thumbnail 4 <input type="file" name="thumb4" /><br />
<br />
large 1 <input type="file" name="large1" /><br />
    large 2 <input type="file" name="large2" /><br />
    large 3 <input type="file" name="large3" /><br />
    large 4 <input type="file" name="large4" /><br />
<input type="submit" name="uploadNew" value="upload!"/>
</form>

 

 

reset() appears to be not working for some strange reason?

what am i missing?

Link to comment
https://forums.phpfreaks.com/topic/187789-key-not-returning-first-value-on-_files/
Share on other sites

reset(), key(), and next() are NOT used with a foreach() loop. The whole purpose of the foreach loop is that IT iterates over the elements of the array and it provides a way of access both the key and the value.

try:

foreach ($_FILES as $key => $value)
{
   // do what you want with $key and $value
}

 

Thanks catfish...for essentially waking me up!  I've used the key=>value structure a thousand times, but my mind got clouded.  It all works fine now.  I don't know what on earth i was thinking?

 

:) have a good weekend.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.