Accessing selected file from files as URL

From this blueprint:

 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:

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

and many other configuration, yet I cant access the URL :frowning:

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

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 ?>

$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…

Your syntax is wrong

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

:cold_sweat:

Oh woops yes, sry php is not my forte.

Thanks so much for your help @texnixe !!!