Single file upload from frontend instead of array

Hello there! Happy new year :slight_smile:

I’ve tried refactoring this:

To work for a single file upload. In the frontend I make use of vue, but it’s mainly a regular html-form with a post request that redirects to the page with the controller. The controller creates a page (this part works) & uploads a cover file (this part doesn’t work).

the vue form:

// 'site.drawlink' links to page with the controller

    <form id="form" :action="site.drawlink" method="POST">
      <input name="file" type="file" required />
      <label for="submitter">Who's submitting?</label>
      <input name="submitter" type="text" placeholder="full name*" required />
      <input name="email" type="email" placeholder="e-mail*" required />
      <input name="date" type="text" :value="fulldate" required hidden />
      <input type="submit" name="register" value="SUBMIT!" />
    </form>

The controller:

<?php
return function ($kirby, $page) {

    if ($kirby->request()->is('POST') === true && get('register')) {

        $data = [
            'submitter'    => get('submitter'),
            'email'    => get('email'),
            'date'    => get('date'),
        ];

        if ($invalid = invalid($data)) {
            $alert = $invalid;
        } else {

            $upload = $kirby->request()->file()->get('file');
            $kirby->impersonate('kirby');

            try {
                $registration = $page->createChild([
                    'slug'     => $data['submitter'] . " " . $data['date'],
                    'template' => 'drawing',
                    'num' => $page->children()->listed()->count() + 1,
                    'content'  => $data,
                ]);                
                try {
                    $name = "cover";
                    $registration->createFile([
                        'source'   => $upload['tmp_name'],
                        'filename' => $name,
                        'template' => 'cover',
                        'content' => [
                            'date' => date('Y-m-d h:m')
                        ]
                    ]);
                } catch (Exception $e) {
                    $alerts[$upload['name']] = $e->getMessage();
                }
                if ($registration) {
                    $registration->changeStatus('unlisted');
                    go('drawings');
                }
            }            
            
            catch (Exception $e) {
                $alert = ['Your registration failed: ' . $e->getMessage()];
                go('error');
            }
        }
    }

    // return data to template
    return [
        'alert' => $alert ?? null,
        'data'  => $data ?? false,
    ];
};

When I submit a completed form I get to the debug page with the message ‘Kirby\Http\Request::file(), 0 passed’

Something I’m misunderstanding?

If you use file() instead of files(), you need to pass a key as parameter, see the documentation.
File:

And then you cannot call get() on it.

1 Like

I made it work with the files instead of file :slight_smile: thanks for the quick reply again!

I do have have a follow-up question, but this could be another post on the forum to keep it separated.