Hello! I followed the example here to create a front-end form that allows users to upload files:
For some reason I get an error with certain images, it seems like it is happening with larger image file sizes but I can’t confirm.
However in the blueprint of the image file, I set the maximum image size to be 8mb to test, and it still causes an error with a file of ~4.5mb:
accept:
mime: image/*
maxsize: 8000000 # size in byte = 8 MB
I’m not sure where in my code to find this error. Here’s the code I used for the submission form controller:
<?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();
}
$uploads = $kirby->request()->files()->get('file');
$data = [
'name' => get('name'),
'email' => get('email'),
'voice' => get('voice')
];
$rules = [
'email' => ['email']
];
$messages = [
'email' => 'Please enter a valid email address'
];
// 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;
}
// some of the data is invalid
if($invalid = invalid($data, $rules, $messages)) {
$alerts = $invalid;
// the data is fine, let's send the email
} else {
try {
$name = crc32($upload['name'].microtime()). '_' . $upload['name'];
$file = page('storage')->createFile([
'source' => $upload['tmp_name'],
'filename' => $name,
'template' => 'upload',
'content' => [
'date' => date('Ymd'),
'username' => $data['name'],
'email' => $data['email'],
'voice' => $data['voice']
]
]);
$success = 'Success! Your image will be processed shortly, please check the IMAGES page in the upcoming weeks.';
} catch (Exception $e) {
$alerts[$upload['name']] = $e->getMessage();
}
}
}
}
return compact('alerts', 'success');
};
Any help would be appreciated, thank you in advance!
Edit: For reference, this submission form is also live at https://readinginteriors.hetnieuweinstituut.nl/submit, if you want to take a look from there.
Could it be something to do with the [‘tmp_name’] in the above code? The error shows this which might give a clue:
Array ( [name] => Array ( [0] => ©CharlottMarkus_Bosmans_Stedelijk_11.jpg ) [type] => Array ( [0] => ) [tmp_name] => Array ( [0] => ) [error] => Array ( [0] => 1 ) [size] => Array ( [0] => 0 ) )