Skip to content

Kirby 5.0.4

$files->find()

Find one or multiple elements by id

$files->find(string ...$keys): Kirby\Cms\File|Kirby\Cms\Files

Parameters

Name Type Default
...$keys string no default value

Return types

Kirby\Cms\FileorKirby\Cms\Files

This method does not modify the existing $files object but returns a new object with the changes applied. Learn more →

Parent class

Kirby\Cms\Files inherited from Kirby\Toolkit\Collection

Examples

Fetch a single file

if ($file = $page->files()->find('myfile.pdf')):
  echo $file->url();
endif;

Fetch multiple files

<ul>
  <?php foreach ($page->files()->find('document-a.pdf', 'document-b.pdf', 'document-c.pdf') as $file): ?>
  <li>
    <a href="<?= $file->url() ?>">
      <?= $file->filename()->html() ?>
    </a>
  </li>
  <?php endforeach ?>
</ul>

Note that you have to pass the file path in relation to the given page if you try to find a specific file within a pages collection.

Finding a file in the sibling files collection

if ($file = $page->siblings()->files()->find('ocean/attention-sharks.jpg')):
  echo $file->url();
endif;

Finding a file in the complete index

if ($file = $site->index()->files()->find('photography/ocean/attention-sharks.jpg')):
  echo $file->url();
endif;