Hi,
I have a site making extensive use of Uniform for form submissions, all working great so far.
I now need to adjust the existing input type="file"
fields to accept multiple files. I have switched them to this:
<input id="filefield" name="filefield[]" multiple type="file">
But am not sure what I need to do to handle multiple files within Uniform. I am happy to have a stab at adjusting the existing uploadAction
but thought I would see if this has already been solved here.
Thanks for any insight anyone has
Have you ever figured this out? Does nobody ever need multiple file uploads?
In the meantime I added an issue to the Uniform repository.
Hi,
I actually did, on a recent project. I have made a note to dig the code out for you tomorrow - can’t promise it’s pretty but it did work!
Ok this is what I came up with - as I said not elegant at all but got the job done. I referenced this while working on it: Forms with attachments | Kirby CMS.
site/controllers/contact.php
<?php
use Uniform\Form;
return function ($kirby, $page)
{
$form = new Form([
'name' => [
'rules' => ['required'],
'message' => 'Please enter your name'
],
'organisation' => [],
'email' => [
'rules' => ['required', 'email'],
'message' => 'Please enter a valid email address',
],
'phone' => [],
'message' => [
'rules' => ['required'],
'message' => 'Please enter a message',
],
'files' => [],
]);
$data = [
'name' => get('name'),
'organisation' => get('organisation'),
'phone' => get('phone'),
'email' => get('email'),
'message' => get('message')
];
// Attachments
$attachments = [];
$alerts = null;
$uploads = $kirby->request()->files()->get('files');
if (!empty($uploads) && count($uploads) > 3) {
$alerts[] = 'You may only upload up to 3 files.';
}
// loop through uploads and check if they are valid
if(!empty($uploads) && count($uploads) > 0):
foreach ($uploads as $upload) {
if($upload['size'] > 0): //Seem to have an empty element thing happening so do this check
// make sure the user uploads at least one file
if ($upload['error'] !== 0) {
$alerts[] = 'The file could not be uploaded';
// make sure the file is not larger than 2MB…
} elseif ($upload['size'] > 5000000) {
$alerts[] = $upload['name'] . ' is larger than 5 MB';
// …and the file is a PDF
} else {
try {
$kirby->impersonate('kirby');
$name = crc32($upload['name'].microtime()). '_' . $upload['name'];
$file = page('storage')->createFile([
'source' => $upload['tmp_name'],
'filename' => $name,
]);
} catch (Exception $e) {
$alerts[$upload['name']] = $e->getMessage();
}
$attachments[] = $file;
}
endif;
}
endif;
if(empty($alerts)):
if ($kirby->request()->is('POST')) {
try {
# Attach file & send email
$form->honeypotGuard()->honeytimeGuard([
'key' => c::get('uniform.honeytime.key'),
'seconds' => 3,
])->emailAction([
'to' => 'contact@website.com',
'from' => 'website@website.com',
'template' => 'simple',
'subject' => 'Website form submission',
'attachments' => $attachments
])->done();
}
catch (Exception $e) {
$form->fail('file', $e->getMessage());
}
}
endif;
return compact('form','alerts','data','uploads');
};
Thanks for digging out the code 
Looks like a great solution and it should even be possible to put this logic into a custom action.
I realised it’s possible to add multiple file fields with the upload action, so I did that (including a plus/minus JavaScript in the frontend).
No worries,
A custom action would definitely be a cleaner solution, I will perhaps take a look at that if I get time.