File factory for a user

Hi,
I am uploading files to a user’s account, using a form on the front end. All is working fine, and the file is appearing in the user’s directory, but I now need to create the .txt file to go with the file itself. I am using the File::factory, but am getting an error when I upload the file. This is my code as it stands:

$file = File::factory([
  'parent'   => kirby()->user()->contentFileDirectory(),
  'filename' => $filename,
  'template' => 'selfie-image',
]);

And I am getting the error:

Argument 1 passed to Kirby\Cms\File::setParent() must be an instance of Kirby\Cms\Model or null, string given

I think this is because my parent setting is incorrect, but I am not sure what to change it to. If anyone has any thoughts that would be great.

The error refers to this line, which needs the user object (so kirby()->user()), not the contentFileDirectory.

Is that your upload code? Or a second step?

It is a second step, I am using Uniform to handle the file upload. Below is my entire controller file for reference:


<?php

use Uniform\Form;

return function ($kirby)
{
    $form = new Form([
        'elfie-selfie' => [
            'rules' => ['required','file','mime' => ['image/jpeg', 'image/png', 'image/heic', 'image/heif']],
            'message' => 'Please send a selfie',
        ],
        'elf' => [],
    ]);

    $random = Str::random(6,'alphaNum');

    if ($kirby->request()->is('POST')) {
      $form->uploadAction(['fields' => [
        'elfie-selfie' => [
            'target' => kirby()->user()->contentFileDirectory(),
            'prefix' => get('elf') . $random . '-',
          ],
      ]]);

      $data = $form->data();
      $prefix = $data['elf'];

      $filefield   = $data['elfie-selfie'];
      if(!empty($data['elfie-selfie']['name'])){
        $filename = $prefix . $random . '-' . $filefield['name'];
      }

      kirby()->user()->update([
        get('elf') => $filename
      ]);

    // At this point the file has arrived in the user's account directory successfully - below is my attempt to generate the .txt file to make it a "proper" Kirby file

      $file = File::factory([
        'parent'   => kirby()->user()->contentFileDirectory(),
        'filename' => $filename,
        'template' => 'selfie-image',
      ]);

    }

    return compact('form');
};

Ah, ok, I thought you were using $user->createFile() in which case you just could have passed the content/template in the array options. But the Uniform upload action just creates the file.

But as I said, use kirby()->user() for the parent.

The File::factory() will only create a file object. What you need to do is update the file, though.

Thanks for the reply. I am still struggling to get this to work I’m afraid. I have changed the parent value but am still not getting the .txt file created. Can you please explain what I need to do to update the file as you describe above?

$file = File::factory([
  'parent'   => kirby()->user(),
  'filename' => $filename,

]);
if ($file) {
  $file->update(['template' => 'selfie-image']);
}

Theoretically, you wouldn’t need the factory, but could fetch the file via the user object

$file = kirby()->user()->file();

if ($file) {
  $file->update(['template' => 'selfie-image']);
}

Thanks, that is now working perfectly