# Working with files from structure fields

**URL:** <https://forum.getkirby.com/t/working-with-files-from-structure-fields/17305>\
**Category:** Questions\
**Tags:** v3\
**Created:** [February 17, 2020, 3:39pm UTC](https://forum.getkirby.com/t/working-with-files-from-structure-fields/17305 "2020-02-17T15:39:23Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Walter](https://avatars.discourse-cdn.com/v4/letter/w/85e7bf/32.png) [@Walter](https://forum.getkirby.com/u/Walter)\
**Post date:** [February 17, 2020, 3:39pm UTC](https://forum.getkirby.com/t/working-with-files-from-structure-fields/17305/1 "2020-02-17T15:39:23Z")

</div>

Hi,  
I’m trying to correctly display files that are attached to specific entries of a structure blueprint field.

The text file has the following structure:

```auto
Events:

- 
  date: 2020-02-21
  title: Workshop 1
  description: 'Lorem sit amet - Stet clita dasd gubergren'
  link:
    - projects/topic-one
  external-link: ""
  files:
    - Test.pdf
  publish: 'true'
- 
  date: 2020-02-12
  title: Workshop 2
  description: 'Sit amet - Stet clita dasd gubergren, Lorem ipsum dolor'
  link: []
  external-link: ""
  files: []
  publish: 'true'
- 

```

I’d like to embed “Test.pdf” as a download link in the rendered html.

My code already iterates through the individual events and displays their title and description:

```
  <div>
    <?php 
    $x = 0;
    foreach ($page->events()->yaml() as $event): ?>
      
      <?php
      $today = date("d-m-Y");

      if ($event['publish'] == 'true') {
      
        if (strtotime($event['date']) < strtotime($today)) { ?>
           
          <h1 class="date">
            <?= str_replace("-", "/", $event['date']) ?>
          </h1>
          <div class="description">
            <?= $event['description'] ?>
          </div>

          <?php
        }
      }
      ?>
    <?php endforeach ?>
  </div>

```

But I cant really get my head around on how to do the same thing for the files. Adding the following ends up in an error message “Call to a member function file() on array”.

```
<?php if($file = $event->file()): ?>
  <?= $file->filename() ?>
<?php endif ?>

```

---

<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:** [February 17, 2020, 4:02pm UTC](https://forum.getkirby.com/t/working-with-files-from-structure-fields/17305/2 "2020-02-17T16:02:59Z")

</div>

```auto
<?php if($file = $event->files()->toFile()): ?>

```

Your field is called `files` not `file`, and you have to convert it to a File object.

But that syntax only works with the `toStructure()` method, you are using `yaml()`. If you want to stick with `yaml`:

```auto
<?php if($file = $page->file($event['files'])): ?>

```

---

<div class="post-metadata">

**Author:** ![Walter](https://avatars.discourse-cdn.com/v4/letter/w/85e7bf/32.png) [@Walter](https://forum.getkirby.com/u/Walter)\
**Post date:** [February 19, 2020, 10:12am UTC](https://forum.getkirby.com/t/working-with-files-from-structure-fields/17305/3 "2020-02-19T10:12:58Z")

</div>

Thanks for the reply!  
Unfortunately I still can’t make it work.

When using the yaml code you’ve posted, I’m prompted with the following error:

> Argument 1 passed to Kirby\Cms\Page::file() must be of the type string or null, array given, called in /Applications/MAMP/htdocs/cmlab-kirby/site/templates/event\_simple.php on line 76

---

<div class="post-metadata">

**Author:** ![bvdputte](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bvdputte/32/8179_2.png) [@bvdputte](https://forum.getkirby.com/u/bvdputte)\
**Post date:** [February 19, 2020, 10:58am UTC](https://forum.getkirby.com/t/working-with-files-from-structure-fields/17305/4 "2020-02-19T10:58:21Z")

</div>

I think you have to pass a parameter to `$page->file()`. E.g. `$page->file("my-file-name.jpg");` or `$page->file(null)`.

---

<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:** [February 19, 2020, 11:29am UTC](https://forum.getkirby.com/t/working-with-files-from-structure-fields/17305/5 "2020-02-19T11:29:21Z")

</div>

Ah, ok, that is because

```auto
$event['files'])

```

doesn’t return a simple string. I think you are better off with using `toStructure()` here:

```auto
<div>
    <?php 
    $x = 0;
    foreach ($page->events()->toStructure() as $event) : ?>
      
      <?php
      $today = date("d-m-Y");

      if ($event->publish()->bool()) :
      
        if ($event->date()->toDate() < strtotime($today)) : ?>
           
          <h1 class="date">
            <?= $event->date()->toDate('Y/m/d') ?>
          </h1>
          <div class="description">
            <?= $event->description() ?>
          </div>

          <?php endif ?>

       <?php endif ?>
    <?php endforeach ?>
  </div>

```

Then you should be able to use

```auto
<?php if($file = $event->files()->toFile()): ?>

```

---

<div class="post-metadata">

**Author:** ![Walter](https://avatars.discourse-cdn.com/v4/letter/w/85e7bf/32.png) [@Walter](https://forum.getkirby.com/u/Walter)\
**Post date:** [February 19, 2020, 11:40am UTC](https://forum.getkirby.com/t/working-with-files-from-structure-fields/17305/6 "2020-02-19T11:40:51Z")

</div>

Thanks,  
this renders a file URL. But only shows the same URL from the first field for each of the following fields.

---

<div class="post-metadata">

**Author:** ![Walter](https://avatars.discourse-cdn.com/v4/letter/w/85e7bf/32.png) [@Walter](https://forum.getkirby.com/u/Walter)\
**Post date:** [February 19, 2020, 11:43am UTC](https://forum.getkirby.com/t/working-with-files-from-structure-fields/17305/7 "2020-02-19T11:43:54Z")

</div>

Hm, weird. Using your code and switching to `toStructure()` resolves the error message but doesn’t show any file links. It seems like the if statement simply never executes.

---

<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:** [February 19, 2020, 11:48am UTC](https://forum.getkirby.com/t/working-with-files-from-structure-fields/17305/8 "2020-02-19T11:48:36Z")

</div>

Please post your complete resulting code.

---

<div class="post-metadata">

**Author:** ![Walter](https://avatars.discourse-cdn.com/v4/letter/w/85e7bf/32.png) [@Walter](https://forum.getkirby.com/u/Walter)\
**Post date:** [February 19, 2020, 3:02pm UTC](https://forum.getkirby.com/t/working-with-files-from-structure-fields/17305/9 "2020-02-19T15:02:58Z")

</div>

Here you go

```
<div>
  <?php 
  $x = 0;
  foreach ($page->events()->toStructure() as $event) : ?>
      
    <?php
    $today = date("d-m-Y");

    if ($event->publish()->bool()) :
      
      if ($event->date()->toDate() < strtotime($today)) : ?>
           
        <h1 class="date">
          <?= $event->date()->toDate('Y/m/d') ?>
        </h1>
        <div class="description">
          <?= $event->description() ?>

          <?php if($file = $event->files()->toFile()): ?>
            <?= $file->filename() ?>
            <p>Test</p>
          <?php endif ?>

        </div>

       <?php endif ?>

     <?php endif ?>
  <?php endforeach ?>
</div>
```

---

<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:** [February 19, 2020, 3:09pm UTC](https://forum.getkirby.com/t/working-with-files-from-structure-fields/17305/10 "2020-02-19T15:09:09Z")

</div>

I can’t see why the code should not do what it is supposed to to. Unless you have more than one file per files field, in which case we would have to change from `toFile()` to `toFiles()` and use another loop. But from what I understood, there is only one file per structure item?

Do you get any output at all?

---

<div class="post-metadata">

**Author:** ![Walter](https://avatars.discourse-cdn.com/v4/letter/w/85e7bf/32.png) [@Walter](https://forum.getkirby.com/u/Walter)\
**Post date:** [February 19, 2020, 4:51pm UTC](https://forum.getkirby.com/t/working-with-files-from-structure-fields/17305/11 "2020-02-19T16:51:24Z")

</div>

Ouch, my bad. I only added a file name to the field in the content file but didn’t add the actual file in the folder. That’s why nothing was showing up.

It works now. Thanks so much for your help!
