Skip to content

Kirby 4.5.0

$file->changeName()

Renames the file (optionally also the extension).

$file->changeName(string $name, bool $sanitize = true, ?string $extension = null): Kirby\Cms\File

Parameters

Name Type Default
$name * string
$sanitize bool true
$extension string|null null

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

Parent class

Kirby\Cms\File

Details

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.

Example

if($file = $page->file('file-with-old-name.pdf')):

    try {

      $newFile = $file->changeName('new-name.pdf');
      echo 'The file has been renamed';

    } catch(Exception $e) {

      echo 'The file could not be renamed';
      // optional reason: echo $e->getMessage();

    }
endif;