I’m trying to combine two Cookbooks. Having started with Basic Contact Form I’m now trying to add a file upload from Uploading file from the frontend.
The form submits with no errors, however it doesn’t seem to be uploading the file. My controller looks like this:
<?php
return function($kirby, $pages, $page) {
$alert = null;
if($kirby->request()->is('POST') && get('submit')) {
// check the honeypot
if(empty(get('website')) === false) {
go($page->url());
exit;
}
$uploads = $kirby->request()->files()->get('file');
// we only want 3 files
if (count($uploads) > 3) {
$alerts['exceedMax'] = 'You may only upload 3 files.';
return compact('alerts', 'success');
}
// authenticate as almighty
$kirby->impersonate('kirby');
foreach ($uploads as $upload) {
// check for duplicate
$files = page('storage')->files();
$duplicates = $files->filter(function ($file) use ($upload) {
// get original safename without prefix
$pos = strpos($file->filename(), '_');
$originalSafename = substr($file->filename(), $pos + 1);
return $originalSafename === F::safeName($upload['name']) &&
$file->mime() === $upload['type'] &&
$file->size() === $upload['size'];
});
if ($duplicates->count() > 0) {
$alerts[$upload['name']] = "The file already exists";
continue;
}
try {
$name = crc32($upload['name'].microtime()). '_' . $upload['name'];
$file = page('storage')->createFile([
'source' => $upload['tmp_name'],
'filename' => $name,
'template' => 'upload',
'content' => [
'date' => date('Y-m-d h:m')
]
]);
$success = 'Your file upload was successful';
} catch (Exception $e) {
$alerts[$upload['name']] = $e->getMessage();
}
}
// Form Data
$data = [
'name' => get('name'),
'firma' => get('firma'),
'telefon' => get('telefon'),
'email' => get('email'),
'baugruppe' => get('baugruppe'),
'nachricht' => get('nachricht'),
'dienstleistungen' => get('dienstleistungen'),
'leiterplatten' => get('leiterplatten'),
'bauteile' => get('bauteile'),
'mechanische' => get('mechanische'),
'baugruppentyp' => get('baugruppentyp')
];
$rules = [
'name' => ['required', 'min' => 3],
'email' => ['required', 'email'],
'nachricht' => ['required', 'min' => 3, 'max' => 3000],
];
$messages = [
'name' => 'Please enter a valid name',
'email' => 'Please enter a valid email address',
'nachricht' => 'Please enter a text between 3 and 3000 characters'
];
// some of the data is invalid
if($invalid = invalid($data, $rules, $messages)) {
$alert = $invalid;
// the data is fine, let's send the email
} else {
try {
$kirby->email([
'template' => 'email',
'from' => 'form@kaempfe-elektronik.de',
'replyTo' => $data['email'],
'to' => 'hello@jamiehunter.design',
'subject' => esc($data['name']) . ' sent you a message from your contact form',
'data' => [
'name' => esc($data['name']),
'firma' => esc($data['firma']),
'telefon' => esc($data['telefon']),
'email' => esc($data['email']),
'baugruppe' => esc($data['baugruppe']),
'dienstleistungen' => $data['dienstleistungen'],
'leiterplatten' => $data['leiterplatten'],
'bauteile' => $data['bauteile'],
'mechanische' => $data['mechanische'],
'baugruppentyp' => $data['baugruppentyp'],
'nachricht' => esc($data['nachricht'])
]
]);
} catch (Exception $error) {
$alert['error'] = "The form could not be sent";
}
// no exception occured, let's send a success message
if (empty($alert) === true) {
$success = 'Ihre Nachricht wurde gesendet, vielen Dank. Wir werden uns bald bei Ihnen melden!';
$data = [];
}
}
}
return [
'alert' => $alert,
'data' => $data ?? false,
'success' => $success ?? false
];
};
I have a separate ‘storage’ page set up. Can anyone see where I’m going wrong?