Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

CDNxL's avatar
Level 1

Rename file if exists

Hi, i use Storage::putFileAs($folder, $file, 'myfilename') for storing files with a custom name.

But if "myfilename" already exists it will be replaced with the new file, there are some options or built-in tricks for adding a number at end of the file name if it already exists?

Or I need to use a custom code?

Thanks

0 likes
3 replies
ejdelmonico's avatar

How are you determining the myfilename? You could use UUIID hash or let Laravel name it or add a timestamp.

CDNxL's avatar
Level 1

@ejdelmonico The name will be chosen by users, but they don't know if another file exists.

So there are no built-in tricks for doing this? Do I need to check if the file exists then append something unique to the base name?

Please or to participate in this conversation.