PHP groups files sent with the same name in an intuitive way, I created a function (https://gist.github.com/pedrosancao/9100827) which interprets the received values in a way that allows the treatment as suggested in the documentation Upload files with the POST method be the same regardless if you receive one or more files.
By 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
Also how the error was reported Undefined index: pic your form should not contain the attribute enctype="multipart/form-data"
, which must be causing part of the problem.
Function setting in case the above link 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;
}
Please avoid long discussions in the comments; your talk was moved to the chat
– Math