Skip to content

Kirby 4.5.0

$file->update()

Updates the file's data and ensures that media files get wiped if focus changed

$file->update(?array $input = null, ?string $languageCode = null, bool $validate = false): Kirby\Cms\File

Parameters

Name Type Default
$input array|null null
$languageCode string|null null
$validate bool false

Return type

Kirby\Cms\File

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

Exceptions

Type Description
Kirby\Exception\InvalidArgumentException If the input array contains invalid values

Parent class

Kirby\Cms\File

Example

Kirby's objects are immutable. That means, when you modify an object like $page, $file etc. using a method like update(), changeTitle() and so on, a new object is returned. Therefore, you have to store the returned object in a new variable to be able to further work with it.

if($file = $page->file('myimage.jpg')) {

  try {

    $newFile = $file->update([
      'caption' => 'This is a really nice image',
      'year'    => 2014
    ]);

    echo 'The meta info has been updated';

  } catch(Exception $e) {

    echo 'The meta info could not be updated';
    // optional reason: echo $e->getMessage();

  }

}

Multi-language installation

In a multi-language installation, you can pass the language of the meta data you want to update as a second argument:

if($file = $page->file('some-file.jpg')) {
  $newFile = $file->update([
    'some_field' => 'new value'
  ], 'en');
}

if($file = $page->file('some-file.jpg')) {
  $newFile = $file->update([
    'some_field' => 'new value'
  ], site()->language()->code());
}