Wouldn’t it be nice if, when you uploaded a file, the duplicate filenames just get a counter added in front?

Some people add timestamps on their files, and add some type of date, but I always found that to be very cumbersome.

How about something like this?


Well, now you can! I have used this little script a couple times to remove any conflicting filenames when uploading files with the same name.

I use this in Laravel (with an instance of Symfony\Component\HttpFoundation\File\UploadedFile which extends SplFileInfo) but it can easily be modified for any other framework or system.

If you are OCD, and wanted to all files to start with a number, just remove the if statement for the $client_name, only leaving the string concatenate line, and it will always add a the counter when the file is new.

So give it a shot, and if you modify it for some other framework, post the link in the comments.