Hi,
I'm building a script that allows a person to upload pictures into a folder on a website (some of the pictures will be GIF's and some will be JPEG's).
The theme of the website is cars and there is a reasonable chance that the user could attempt to upload several pictures that all have the same filename (eg, "volvo.jpg").
I am therefore trying to create some kind of function that reads the name of the incoming file, checks the folder for files with that same name and IF the folder already contains a file with that name then a number is added to the end of the incoming file name.
So, in other words, if somebody uploads 7 different pictures that are all called "volvo.jpg" then I would ideally like the files to be renamed as:
volvo.jpg
volvo2.jpg
volvo3.jpg
volvo4.jpg
volvo5.jpg
...and so on
I've been wrestling with this for two days, but so far I can only make it rename the files as...
volvo.jpg
volvo2.jpg
volvo22.jpg
volvo222.jpg
volvo2222.jpg ...etc
The script that I've written uses a do-while statement something like this...
If (the file called $incoming_picture already exists) {
$count=1;
do {
$incoming_picture=str_replace(".", "$count.",
$count++;
} while (the file still exists in the folder);
}
...but of course, it doesn't work quite right.
Botton line: Can anyone out there please help me to rename duplicate files in the way that I've described above? 🙂