Image upload from front-end form not working

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 ) )

The problem rather seems to be that for some reason the type field in the upload array is empty

Hi pixelijn! Can you elaborate on this? Why is it that some images work and others don’t, even if they are the same file type and file name etc?

I am testing with only screenshots (so the file name and type are the same) and for some reason anything under 2mb works, anything over gives me this error. But not sure how this affects the type field.

Then it is a probably a problem with your php.ini settings, check post_max_size and upload_max_size.

Hi @texnixe,

Hmm I looked into the php.ini settings and the upload_max_size is indeed 2M. I changed both settings to 8mb just to be sure, but I’m still getting this error. Is there something else that looks off? Maybe it is indeed the type field.

Thanks for your help!

Turns out the server just had to be restarted, all works now! Thanks @texnixe for the tip!