I am aware that this is an old article, but anyone attempting to upload numerous files might find some more explanation helpful. What you must do is as follows:
- Input name must be be defined as an array i.e. name="inputName[]".
- Input element must have multiple="multiple" or just multiple.
- In your PHP file use the syntax "$_FILES['inputName']['param'][index]".
- Make sure to check the array for empty strings; these could be file names or path entries. Prior to counting, use an array filter().
Here is an example:
HTML:
<input name="upload[]" type="file" multiple="multiple" />
PHP:
//$files = array_filter($_FILES['upload']['name']); //something like that to be used before processing files.
// Count # of uploaded files in array
$total = count($_FILES['upload']['name']);
// Loop through each file
for( $i=0 ; $i < $total ; $i++ ) {
//Get the temp file path
$tmpFilePath = $_FILES['upload']['tmp_name'][$i];
//Make sure we have a file path
if ($tmpFilePath != ""){
//Setup our new file path
$newFilePath = "./uploadFiles/" . $_FILES['upload']['name'][$i];
//Upload the file into the temp dir
if(move_uploaded_file($tmpFilePath, $newFilePath)) {
//Handle other code here
}
}
}
I hope this helps you.