Skip to content

Kirby 5.0.4

$file->changeName()

Renames the file (optionally also the extension). The store is used to actually execute this.

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

Parameters

Name Type Default
$namerequired string no default value
$sanitize bool true
$extension stringornull 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

Examples

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();
    }
}