Deleted audio file still visible in frontend

Hi,

I have a files field for audio files which works fine when the file is uploaded, but when i delete it it’s still visible. what’s wrong here? problem occurs on the live server (php 7.4).

blueprint:

  audio:
    type: files

template:

<?php if ($page->audio()->isNotEmpty()): ?>
    <div>
    <h2>Audio</h2>
<?php foreach($page->audio() as $audio): ?>
<audio controls>
  <source src="<?= $audio->url() ?>" type="<?= $audio->mime() ?>">
</audio>
<?php endforeach ?>
</div>

<?php endif ?>

The reason why your code works at all is that audio is a custom Kirby method that fetches all audio files from your page, not the ones selected in your files field. If you were fetching the field, the code wouldn’t work, because you can’t loop through a field object.

What you have to do, is convert your field content into a file object (single file) or files collection.

<?php
// because `audio` is a native page method, we have to go via the content object (or you have to rename your field to something else)
$audioFiles = $page->content()->get('audio')->toFiles();
if ( $audioFiles->isNotEmpty() ) : ?>
<div>
    <h2>Audio</h2>
<?php foreach($audioFiles as $audio): ?>
<audio controls>
  <source src="<?= $audio->url() ?>" type="<?= $audio->mime() ?>">
</audio>
<?php endforeach ?>
<?php endif; ?>

Aah, perfect! Thanks @texnixe :slight_smile: