File.create:before hook – changeName

Hi! I’m having the same issue as this person Uploading duplicate files,
meaning I’m trying to rename a file by adding a timestamp before upload with a file create hook. The reason behind is that if I have 2 file sections, you can’t have an image test.png in file section 1 and another test.png in file section 2. It will show a the file is already existing and can’t be overwritten error.

'file.create:before' => function ($file, $upload) {
   $page = $file->page();
   $timestamp = time();
   $filename = $file->name() . "_test_" . substr($timestamp, -5);

(I tried rename() as well)
It works on uploads with new filenames but not with duplicate/existing filenames. :confused:
The issue persists with an :after hook.

I read on the mentioned thread, that this issue goes away if you use file fields instead of sections but since the page coding is already quite far, it’ll take some time to change all the code and I’m trying to avoid changing fields/sections.

Is there any trick to make it work? Thx!

changeName() is correct, but you can’t do this in a before hook, but have to do it in an after hook.

I tried that to no avail before already :confused:

Oh ok, I think I figured it out. I have to upload also the existing images (the ones which have been uploaded before the file.create:after hook was in place).