Been working alot (and will be for a while more) so I don’t really want to devote the time needed for a real post, but this little tidbit saved me a bunch of trouble yesterday and I think it’s important enough to post.
HTML5 supports multiple selection of form inputs by adding the tag
multiple="multiple" to the input. Only problem is that it PHP seems to want to take the last item from that list (since to PHP it’s seeing 5
$_POST items all with the same name, each one overwriting the one that came before it). The fix for this is two parts.
Part one is to form your input names so that PHP turns them into arrays. For example I used this for a image upload, it was originally was
name="image" but by adding square brakets to the end of it like this
name="image" when PHP saw that it placed it in an array, and in the case of the a file upload like I used for the images, it was all stored in the
$_FILES array, making it look something like this:
$_FILES['image'] = array("name" => array(0=>"firstfile.jpg", 1=>"secondfile.jpg", 2=>"thirdfile.jpg", 3=>"ect.jpg"),
/* ... and all the other attributes go here ... */
Now I didn’t want to reinvent the wheel (or in this case reprogram the upload script) so I wanted to retrofit this into the upload field. The simplest way I could thing of is to count the number of items in the name field (as it’ll always have one for each) and then running the script once per file (using a
while loop to count
$i up to the maximum number of files as gotten by counting the items earlier) Then all I had to do was add
[$i] to the end of all the calls that involve the
$_FILES array and it grabbed the appropriate information. Simple as Simple Can be.
I know this is a messy post and not very well though through but I will make a more detailed posting, with appropriate example and Snipplr as soon as my contract at FR is complete, how long will that take? Don’t know, but should be to long.
I’d say that none of the code was testing, but there isn’t any, none of the variable names were checked, just going off of what I remember from yesterday/this morning from EM, like I said, better more detailed instructions soon.