Hi,
I’m trying to combine two cookbook recipes:
Everything works fine so far – the only thing is that it throws "error.file.mime.missing"
alert upon submission, even though the page is created and files are uploaded as its children successfully.
“The media type for “img-1-0.45521000-1617410187” cannot be detected”
Is there anything I’m missing? var_dump($upload[$i])
also shows me ["type"]=> string(10) "image/jpeg"
…
Thanks!
<?php
return function ($kirby, $page) {
$alerts = [];
$success = '';
if ($kirby->request()->is('post') === true && get('submit')) {
// check the honeypot
if (empty(get('website')) === false) {
go($page->url());
exit();
}
$data = [
'firstname' => get('firstname'),
'lastname' => get('lastname'),
'name' => get('firstname') . get('lastname'),
'alphabetize' => get('lastname'),
'featured' => false,
'netid' => get('email'),
'title' => get('title'),
'text' => get('text'),
'timestamp' => time(),
];
$upload[0] = $kirby->request()->files()->get('file_1');
$upload[1] = $kirby->request()->files()->get('file_2');
$upload[2] = $kirby->request()->files()->get('file_3');
$upload[3] = $kirby->request()->files()->get('file_4');
$upload[4] = $kirby->request()->files()->get('file_5');
$rules = [
];
$messages = [
];
// some of the data is invalid
if ($invalid = invalid($data, $rules, $messages)) {
$alert = $invalid;
}
else {
// authenticate as almighty
$kirby->impersonate('kirby');
try {
// create page first
$submission = $page->createChild([
'title' => $data['title'],
'slug' => str::slug($data['name']),
'template' => 'project',
'content' => $data
]);
$submission->changeStatus('listed');
// handle uploads
for ($i = 0; $i < count($upload); $i++) {
$name = str::slug($data['name']).'--'. $i .'--'. microtime() .'--'. $upload[$i]['name'];
$file = page($submission->id())->createFile([
'source' => $upload[$i]['tmp_name'],
'filename' => $name,
]);
}
$success = 'Your file upload was successful';
}
catch (Exception $e) {
$alerts[$upload['name']] = $e->getMessage();
}
}
}
return compact('data', 'alerts', 'success');
};