fivestringsurf Posted January 9, 2010 Share Posted January 9, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/187789-key-not-returning-first-value-on-_files/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 9, 2010 Share Posted January 9, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/187789-key-not-returning-first-value-on-_files/#findComment-991487 Share on other sites More sharing options...
Catfish Posted January 9, 2010 Share Posted January 9, 2010 try: foreach ($_FILES as $key => $value) { // do what you want with $key and $value } Quote Link to comment https://forums.phpfreaks.com/topic/187789-key-not-returning-first-value-on-_files/#findComment-991488 Share on other sites More sharing options...
fivestringsurf Posted January 9, 2010 Author Share Posted January 9, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/187789-key-not-returning-first-value-on-_files/#findComment-991517 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.