Uniform file sending problem

When I try to send a form with some files using Uniform, I always get an error message that the file format is not correct, even if the file format is right.

This is the controller code:

<?php

use Uniform\Form;

return function ($kirby) {
   $form = new Form([
      'bewerbungals' => [
	      'rules' => ['required'],
         'message' => 'Geben Sie die Stelle ein, für die Sie sich bewerben',
      ],
      'name' => [
	      'rules' => ['required'],
         'message' => 'Geben Sie Ihren Namen ein',
      ],
      'strasse' => [
	      'rules' => ['required'],
         'message' => 'Geben Sie Ihre Strasse und Hausnummer ein',
      ],
      'plzort' => [
	      'rules' => ['required'],
         'message' => 'Geben Sie Ihren Wohnort und Ihre Postleitzahl ein',
      ],
      'telefon' => [
	      'rules' => ['required'],
         'message' => 'Geben Sie Ihre Telefonnummer ein',
      ],
      'fax' => [],
      
      'email' => [
         'rules' => ['required', 'email'],
         'message' => 'Geben Sie Ihre Emailadresse ein',
      ],
      
      'filefield1' => [
            'rules' => [
                'required',
                'file',
                'mime' => ['application/pdf'],
                'filesize' => 3000,
            ],
            'message' => [
                'Please choose a file.',
	            'Bitte senden Sie ausschliesslich Dateien.',
                'Bitte senden Sie ausschliesslich PDF, Word und Jpeg-Dateien.',
                'Die gesendeten Dateien müssen kleiner als 3MB sein.',
            ],
        ],
      
      'filefield2' => [
            'rules' => [
                'required',
                'file',
                'mime' => ['application/pdf'],
                'filesize' => 3000,
            ],
            'message' => [
                'Please choose a file.',
	            'Bitte senden Sie ausschliesslich Dateien.',
                'Bitte senden Sie ausschliesslich PDF, Word und Jpeg-Dateien.',
                'Die gesendeten Dateien müssen kleiner als 3MB sein.',
            ],
        ],
      
      'filefield3' => [
            'rules' => [
                'required',
                'file',
                'mime' => ['application/pdf'],
                'filesize' => 3000,
            ],
            'message' => [
                'Please choose a file.',
	            'Bitte senden Sie ausschliesslich Dateien.',
                'Bitte senden Sie ausschliesslich PDF, Word und Jpeg-Dateien.',
                'Die gesendeten Dateien müssen kleiner als 3MB sein.',
            ],
        ],
      
      'datenschutz' => [
          'rules' => ['required'],
          'message' => 'Markieren Sie die Checkbox für die Datenschutzerklärung',
      ],
        
   ]);
   
  
 // get the uploads
        $upload1 = $kirby->request()->files()->get('filefield1');
        $upload2 = $kirby->request()->files()->get('filefield2');
        $upload3 = $kirby->request()->files()->get('filefield3');
	
        $uploads = array($upload1, $upload2, $upload3);
	
	
  // loop through uploads and check if they are valid
        


   if ($kirby->request()->is('POST')) {
	  foreach ($uploads as $upload) {

		// if no file is uploaded (its not required)
            if ($upload['error'] === 4) {

                	// do nothing
          
            } else {

           
                $name     = $upload['tmp_name'];
                $tmpName  = pathinfo($name);
                // sanitize the original filename
                $filename = $tmpName['dirname']. '/'. F::safeName($upload['name']);

                if (rename($upload['tmp_name'], $filename)) {
                    $name = $filename;
                }
                // add the files to the attachments array
                $attachments[] = $name;
            }  
        }; 
	  
	   
      $form->emailAction([
	     'template' => 'email',
         'to' => 'guy@t-online.de',
         'from' => 'bewerbung@job.de',
         'subject' => 'Neue Bewerbung von {{email}}',
         'attachments' => $attachments,
      ]);   
   };

   return compact('form');
};

Strangely enough, if I send the form without a file, I get the second error message (‘Bitte senden Sie ausschliesslich Dateien.’). Shouldn’t that be the first one in this case (for ‘required’)?

What could be the reason for this?

What do you get if you send files and then dump $uploads?

An empty Array:

Array
(
    [0] => 
    [1] => 
    [2] => 
)

If i remove the rules ‘required’ and ‘file’ the form is send as an email with all the files, but the file recognition with mime is not accounted for, you can send any filetype.

Hm, could you send me the (reduced) project via a download link for testing?

I would be grateful to do so, thank you very much.

Can I send the link to the email address in your website?

Yes, you can do either that or send me a PM here.

I have sent the link to the email address.

Got it!

Hm, I don’t know what the problem is here, if I use the upload action instead of the email action, file validation works as expected.

I have never sent attachments with Uniform, only using the Kirby way as in this recipe: https://getkirby.com/docs/cookbook/forms/email-with-attachments

If I use the upload action instead of the email action, the validation still does not work. PDFs are not recognized. Did you change anything else?

Well, I simplified the whole form to have only a single form field and two upload fields because I hate testing with lots of unnecessary stuff… But apart from that, not really.

Thanks for your help. I’m going to use the “email with attachments” method instead.
I honestly don’t see why you would need to upload the form content with attachments to Kirby. It’s IMHO much more convenient to have everything in one email.

No, that is in fact not necessary.