Posted on March 25th, 2010 at 03:41 PM by Corey Ballou
PHP inherently makes parsing an array of uploaded files more difficult than it needs to be due to the ordering of it’s array indices. Below is a quick example array:
$_FILES['fieldname']['name'][1] = 'uploadedfile.jpg'; $_FILES['fieldname']['name'][2] = 'uploadedfile2.jpg'; $_FILES['fieldname']['type'][1] = 'image/jpeg'; $_FILES['fieldname']['type'][2] = 'image/jpeg'; $_FILES['fieldname']['tmp_name'][1] = '/tmp/rAnDOmCHaRs'; $_FILES['fieldname']['tmp_name'][2] = '/tmp/RANdOmcHArs'; $_FILES['fieldname']['error'][1] = 0; $_FILES['fieldname']['error'][2] = 0; $_FILES['fieldname']['size'][1] = 1427; $_FILES['fieldname']['size'][2] = 1576;
To combat this, we could create a function to reassemble the multi-dimensional array so that it is based on index rather than key. This allows for easier iteration of files. Here’s an example of the reindex $_FILES['fieldname'] array: more »