Skip to content

Kirby 4.5.0

$files

The $files object extends the general Collection class and refers to a collection of files, i.e. images, documents etc. Files can be filtered, searched, converted, modified or evaluated with the following methods:

You can extend this set of methods with custom files methods.

How to get a $files object

Before you can use the methods of the $files class, you need a $files object. To get a files object, you fetch the files you need from the $site, $page or $user object:

Note that the default sorting order of files is according to their order in the file system. To sort files by their manual sorting order, you can use $files->sortBy('sort'), where sort is the field that stores the sorting number if you manually sort files in the Panel.

Site files

$files = $site->files();

Page files

$files = $page->files();

User files

$files = $user->files();

Examples:

With the $files object defined, you can now do all sorts of things:

Filter files

$filteredFiles = $files->filterBy('template', 'cover');

Group files

$groupedFiles = $files->groupBy('template');

Get the first file of the collection

$firstFile = $files->first();

Only get three files

$files = $files->limit(3);