The uploads folder currently contains one file and note the last modified time.
If I upload a file with the same name.
Then the existing file with the same name is overwritten.
Note how the time has changed.
We don't want this because someone could upload a file with the same name as an existing file overwriting
its contents.
The move uploaded file function overrides any existing files.
So before we do this we need to check if the file exists.
We can do this using the file exists.
Function.
If the file does exist instead of changing it to something random Let's add a numeric suffix to the
original file name we'll add this before the file extension.
This file could also exist however so we need to keep checking if these files exist.
Adding in incrementing numeric suffix until we find a file name that doesn't exist so that we can use
that to move the uploaded file.
We can do this using a while loop this checks to see if the file exists.
If it doesn't then we don't enter this while loop and the file is moved to the uploads folder with the
original file name.
If it does exist we need to add to the numeric suffix to the file name.
We're creating the original file name but here.
So let's copy this line and paste it inside the while loop.
Before we enter the loop will initialize a variable to the integer value of 1.
Then when we create the file name inside the loop will insert a hyphen and this value to the end of
the file name before the period and the file extension.
Then we create the full path to the destination as before.
And finally in comments the counter.
Let's give that a try starting with this file in the uploads folder.
If I upload a file with the same name.
The file has been loaded with a numeric suffix and the original file hasn't been overwritten.
If I upload it again.
Then again it's saved with a different file name.