1

When I select a single file or multiple files, the result is the same. I am expecting this code to upload multiple files. But the array is empty. I have reviewed multiple posts by others and the answers to those questions don't seem to apply here. If I eliminate all form inputs except the hidden input with name 'action' the uploads work just fine.

Here is the relvant HTML form:

<form action="bulletin.php" method="post" enctype="multipart/form-data">
<input class="form-control" id="message_subject" name="message_subject" type="text"/>
<textarea class="form-control" cols="40" id="message_body" name="message_body" rows="10"></textarea>
<input type="file" id="file" name="file[]" multiple accept="image/*"/>
<input type="hidden" name="action" value="submit_new">
<button class="btn btn-primary " name="submit" type="submit">Submit</button>
</form>

Here is the PHP:

print_r($_FILES);       
if(isset($_POST['action'])){

// Count total files
$countfiles = count($_FILES['file']['name']);

// Looping all files
for($i=0;$i<$countfiles;$i++){
$filename = date('Y-m-d-H-i-s') . $_FILES['file']['name'][$i];

// Upload file
move_uploaded_file($_FILES['file']['tmp_name'][$i],'user-images/'.$filename);

}
}

Here is the output:

Array ( )

Notice: Undefined index: file in /var/www/html/secure/bulletin.php on line 66

Why is the array empty when there are other inputs included on form?

David
  • 13
  • 3

1 Answers1

0

So, in my case, there are multiple forms on this php page, but do not display unless the if statement is true. I stumbled across a post on the web that suggested that every form on a page should include the enctype="multipart/form-data". So I went through and added it to every form on the php file and this fixed the mysterious issue. Not sure why unrelated form tags would cause this.

David
  • 13
  • 3