PHP groups uploaded files with the same name in a less intuitive way, I've created a function ( link ) that interprets the received values in a way that allows for the treatment as suggested in the Uploading Files with Method POST is the same regardless of whether you receive one or more files.
When calling parse_files()
, a numeric suffix is added to the form field name, so that the data is as follows:
// estrutura original
echo $_FILES['pic']['name'][0]; // exemplo1.jpg
echo $_FILES['pic']['name'][1]; // exemplo2.jpg
// estrutura depois do tratamento
$files = parse_files();
echo $files['pic_0']['name']; // exemplo1.jpg
echo $files['pic_1']['name']; // exemplo2.jpg
In addition, as the Undefined index: pic error was reported, your form should not contain the enctype="multipart/form-data"
attribute, which should be causing some of the problem.
Function definition if link above is inaccessible.
<?php
/**
* Parses the PHP's $_FILES array when multiple file input is used, it will
* return an array as each file was from a different input. It also works
* when multiple and single inputs are mixed
*
* @author Pedro Sanção
* @license MIT Licence
*/
function parse_files() {
$files = array();
foreach ($_FILES as $fieldName => $file) {
reset($file);
$key = key($file);
if (is_array($file[$key])) {
$propeties = array_keys($file);
$fieldNamekeys = array_keys($file[$key]);
foreach ($fieldNamekeys as $namekey) {
$fileKey = "{$fieldName}_{$namekey}";
$files[$fileKey] = array();
foreach ($propeties as $property) {
$files[$fileKey][$property] = $file[$property][$namekey];
}
}
} else {
$files[$fieldName] = $file;
}
}
return $files;
}