# Programatically upload files with template, and update \`files\` field with these files

**URL:** https://forum.getkirby.com/t/programatically-upload-files-with-template-and-update-files-field-with-these-files/28154
**Category:** Questions
**Tags:** v3
**Created:** [March 21, 2023, 9:54am UTC](https://forum.getkirby.com/t/programatically-upload-files-with-template-and-update-files-field-with-these-files/28154 "2023-03-21T09:54:05Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![plagasul](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/plagasul/32/4199_2.png) [@plagasul](https://forum.getkirby.com/u/plagasul)
#### Post date: [March 21, 2023, 9:54am UTC](https://forum.getkirby.com/t/programatically-upload-files-with-template-and-update-files-field-with-these-files/28154/1 "2023-03-21T09:54:05Z")

</div>

I have a folder full of images that:

- I have to upload to a series of pages of `work` type
- Using `image` file template
- And assign them to a `files` field, titled `cover`.

Each page has one `cover` field, with a single image from the batch.

How do I programatically do this? Do I have to separate the upload file and update field code?

If there are docs for this, please help me find them

Thanks

---

<div class="post-metadata">

### Author: ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)
#### Post date: [March 21, 2023, 12:03pm UTC](https://forum.getkirby.com/t/programatically-upload-files-with-template-and-update-files-field-with-these-files/28154/2 "2023-03-21T12:03:10Z")

</div>

These two are quite similar and should help you get going:

> [@Save external image from url in kirby 3](https://forum.getkirby.com/t/save-external-image-from-url-in-kirby-3/27301/3):
>
> yes I have been going through the PHP classes but it is not working. let me try again with only those two params. I also tried this $image = $page-\>createFile(['source' =\> $external\_url, 'parent' =\> $exhibit-\>parentModel(), 'filename' =\> $imageName, 'template' =\> 'exhibit\_preview',]); but then it did not work either. ok i just tried it and it is the same. no errors are thrown or anything, but the txt file is not there c\_exhibit is…

> [@$page-\>update() not working correctly for files fields](https://forum.getkirby.com/t/page-update-not-working-correctly-for-files-fields/28051/3):
>
> Additionally, a files field expects data as an array, not a string Also, at this point Kirby doesn’t know that this file exist yet as it is not in the inventory, so it won’t be stored. You have to add it to the inventory first.

Also this recipe: [Uploading files | Kirby CMS](https://getkirby.com/docs/cookbook/forms/file-uploads)

---

<div class="post-metadata">

### Author: ![plagasul](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/plagasul/32/4199_2.png) [@plagasul](https://forum.getkirby.com/u/plagasul)
#### Post date: [March 22, 2023, 10:34am UTC](https://forum.getkirby.com/t/programatically-upload-files-with-template-and-update-files-field-with-these-files/28154/3 "2023-03-22T10:34:59Z")

</div>

@texnixe I am getting a strange exception: `The method: "alt" does not exist` with this code:

```
        ...
        $img = asset('assets/import/img/' . $path . '.jpg');

        if ($img->exists()) {
            echo 'file ' . $img->filename() . ' exists, proceed to upload<br />';
            try {
                $w->createFile([
                    'source' => $img,
                    'filename' => $img->filename(),
                    'template' => 'focusimg',
                ]);
                echo 'success <br />';
            } catch (Exception $e) {
                echo $e->getMessage() . '<br />';
            }
            
        } else {
            "file does not exist <br /><br />";
        }

```

It seems the exception is thrown when reaching the `'template' => 'focusimg'` line.

That blueprint looks like this:

title: focus image  
accept: image/\*

```auto
fields:
  alt:
    label: Texto alternativo
    type: text 
    width: 1/1
  focus:
    label: Centro del recorte
    type: focus
    width: 1/2

```

Is it requiring me to fill the alt field using a content array on the createFile method? and if yes, why, if that is not a required field? or that is just a name coincidence between some internal `alt` and my alt field?

Thanks

---

<div class="post-metadata">

### Author: ![plagasul](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/plagasul/32/4199_2.png) [@plagasul](https://forum.getkirby.com/u/plagasul)
#### Post date: [March 22, 2023, 2:36pm UTC](https://forum.getkirby.com/t/programatically-upload-files-with-template-and-update-files-field-with-these-files/28154/4 "2023-03-22T14:36:42Z")

</div>

It seems my mistake was to use asset() to create the source, instead of simply providing the path as a string, this works:

```
        $img = 'assets/import/img/' . $path . '.jpg';

        if (file_exists($img)) {
            echo 'file ' . $img . ' exists, proceed to upload<br />';
            try {
                $w->createFile([
                    'source' => $img,
                    'filename' => $path . '.jpg',
                    'template' => 'focusimg',
                ]);
                echo 'success <br />';
            } catch (Exception $e) {
                echo $e->getMessage() . '<br />';
            }
            
        } else {
            "file does not exist <br /><br />";
        }

```
