[uniform] optional upload of multiple files

hi there!
iā€™ve got an ajax-form (uniform) with a file-input field that should be optional. so without having set the ā€˜requiredā€™ rule, the send-request of a form with an empty fileinput throws the error-message ā€œthe file could not be uploadedā€ while successfully submitting the other form data.

    $form->otherFormAction()
    ->uploadAction(['fields' => [
        'filefield' => [
            'target' => $target,
            'prefix' => false
    ]]]);

to check if the field is (not) empty, i guess, it should be something like this in the config.php:

if (get('filefield')) {
    $form->uploadAction(['fields' => [
        'filefield' => [
            'target' => $target,
            'prefix' => false
    ]]]);
};    

seems that it doesnā€™t work for files, so how can i check if there is an input? (name? filesize?)
thanks!

you can check with the

$_FILES constant instead of get(ā€˜filefieldā€™)

https://secure.php.net/manual/de/reserved.variables.files.php

In your case, you want to check if the filefield index exists and if it contains data, for example, if the name is not empty:

if(isset($_FILES['filefield']) && $_FILES['file field']['name'] !== '') {
    // do stuff
}

thank you!
aaand another question:
if i have one multiple files input like

<input name="filefield" type="file" multiple>

how do i pass them to the uploadaction?
i tried to do this:

if(isset($_FILES['filefield']) && $_FILES['filefield']['size'] != 0) {
    foreach($_FILES['filefield'] as $singlefield) {
        $filefields[] = [
            $singlefield => [
                'target' => $newtarget,
                'prefix' => false,
            ]];
    };
    $form->uploadAction(['fields' => $filefields]);
};

ā€œtarget is not setā€. sorry, i guess itā€™s my php noobness again.

The array you get when you upload multiple files is something like this:

Array
(
    [name] => Array
        (
            [0] => foo.txt
            [1] => bar.txt
        )

    [type] => Array
        (
            [0] => text/plain
            [1] => text/plain
        )

    [tmp_name] => Array
        (
            [0] => /tmp/phpYzdqkD
            [1] => /tmp/phpeEwEWG
        )

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

    [size] => Array
        (
            [0] => 123
            [1] => 456
        )
)

So each of the keys is an array again. You will have to adapt your code accordingly.

Do a dump to see what your resulting arrays look like with and without images:

  if(isset($_FILES['filefield'])) {
      dump($_FILES['filefield']);
  }

ok, i managed to convert the $_FILES array into a common one. now i realised that the upload-action fetches the field-data by field-name, so i could either somehow get multiple names from the formfield, which looks like this:

<input name="filefield[]" type="file" multiple>

or modify the upload-action and pass the files-array directly. now looks like:

Array
(
    [0] => Array
        (
            [name] => a.pdf
            [type] => application/pdf
            [tmp_name] => tmp/phphO1BV9
            [error] => 0
            [size] => 1234
            [target] => /thepage
            [prefix] => false
        )
    [1] => Array
        (
            [name] => b.pdf
            [type] => application/pdf
            [tmp_name] => tmp/pdasIjV9
            [error] => 0
            [size] => 1234
            [target] => /thepage
            [prefix] => false
        )
)

just wondering if this is the right (best) way.

have you found a solution for the upload of multiple files via the upload action?