Rename file on upload hook

Hi!

I’m trying to rename files on upload (Site name + page title + number) but unfortunately this doesn’t work :confused:

Maybe someone has any good tips?

'hooks' => [
	     'file.create:after' => function ($file) {
		      $page = $file->page();
		      $title = $page->title();
              $count = $page->files()->count() + 1;	
              $filename = "Something-" . $title . "-" . $count;
		      try {
		          $file->rename($filename);
		          } catch(Exception $e) {
		    }
	}

(It’s the latest Kirby build.)

Thanks!

Doesn’t it work at all or only when uploading multiple files at once?

Unfortunately not at all :confused:

Try with changeName() instead, rename() has been deprecated.

Perfect!
:+1:
(That explains why it did somehow work with the previous Kirby version.)
Thanks a lot!
Stay healthy!

1 Like

Are you doing this to prevent errors when files have the same name? I am trying to solve the same issue. Does renaming the files have any downsides?