# Accessing selected file from files as URL

**URL:** <https://forum.getkirby.com/t/accessing-selected-file-from-files-as-url/15429>\
**Category:** Questions\
**Tags:** v3\
**Created:** [August 28, 2019, 3:08pm UTC](https://forum.getkirby.com/t/accessing-selected-file-from-files-as-url/15429 "2019-08-28T15:08:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![fumatore](https://avatars.discourse-cdn.com/v4/letter/f/bc79bd/32.png) [@fumatore](https://forum.getkirby.com/u/fumatore)\
**Post date:** [August 28, 2019, 3:08pm UTC](https://forum.getkirby.com/t/accessing-selected-file-from-files-as-url/15429/1 "2019-08-28T15:08:29Z")

</div>

From this blueprint:

```auto
 sidebar:
      width: 2/3
      sections:
        # we need a field section here
        fieldsection:
          type: fields
          fields:
           # a files field to select an image
            featured:
              label: Featured Image # fields have labels, not headlines
              type: files
              max: 1

```

I would like to echo the featured image’s URL. The idea is that the editor uploads all images from the project and then selects one from the group to show as featured image.

The file name is being printed on the project.txt file, and I can echo it, but I cannot turn it into a URl so that it can be embedded as source. What am I doin wrong?

I have tried:

```auto
$page->featured()->url()
$page->images($works->featured())->url()

```

and many other configuration, yet I cant access the URL ☹

---

<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:** [August 28, 2019, 3:17pm UTC](https://forum.getkirby.com/t/accessing-selected-file-from-files-as-url/15429/2 "2019-08-28T15:17:56Z")

</div>

```auto
if ($image = $page->featured()->toFile()) {
  echo $image->url();
}

```

---

<div class="post-metadata">

**Author:** ![fumatore](https://avatars.discourse-cdn.com/v4/letter/f/bc79bd/32.png) [@fumatore](https://forum.getkirby.com/u/fumatore)\
**Post date:** [August 28, 2019, 3:42pm UTC](https://forum.getkirby.com/t/accessing-selected-file-from-files-as-url/15429/3 "2019-08-28T15:42:22Z")

</div>

I get an error with this code

> \<?php foreach ($page-\>children()-\>listed()-\>sortBy('year', 'desc') as $works) : ?\>
> 
> ```
> <div class="col-xs-12 col-sm-6 col-md-4 py-4 work-ft">
> <a href="<?= $works->url() ?>">
> <img class="img-fluid work-ft-img " src="<?= 
> if ($image = $works->featured()->toFile()) {
> echo $image->url();
> }
> ?>">
> </a>
> <?= $works->title()->upper() ?>
> <div class="<?= $works->year() ?>">
> <?= $works->year() ?>
> </div>
> </div>
> 
> <?php endforeach ?>
> 
> ```

---

<div class="post-metadata">

**Author:** ![fumatore](https://avatars.discourse-cdn.com/v4/letter/f/bc79bd/32.png) [@fumatore](https://forum.getkirby.com/u/fumatore)\
**Post date:** [August 28, 2019, 5:18pm UTC](https://forum.getkirby.com/t/accessing-selected-file-from-files-as-url/15429/4 "2019-08-28T17:18:37Z")

</div>

> $image = $works-\>featured()-\>toFile()

prints the file name, but when i try

> $image-\>url()

I get an error. I tried with different variables names, but still the same…

---

<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:** [August 28, 2019, 5:21pm UTC](https://forum.getkirby.com/t/accessing-selected-file-from-files-as-url/15429/5 "2019-08-28T17:21:01Z")

</div>

> [@fumatore](#):
>
> ![]()

Your syntax is wrong

```auto
<?php if ($image = $page->featured()->toFile()): ?>
    <img class="img-fluid work-ft-img " src="<?= $image->url() ?>">
<?php endif ?>

```

---

<div class="post-metadata">

**Author:** ![fumatore](https://avatars.discourse-cdn.com/v4/letter/f/bc79bd/32.png) [@fumatore](https://forum.getkirby.com/u/fumatore)\
**Post date:** [August 28, 2019, 5:26pm UTC](https://forum.getkirby.com/t/accessing-selected-file-from-files-as-url/15429/6 "2019-08-28T17:26:46Z")

</div>

😰

Oh woops yes, sry php is not my forte.

Thanks so much for your help @texnixe !!!
