I wrote some php that checks if a file already exists when a user uploads a new file, and if so appends a string to the filename. What I want is that a number is added to the end of the filename and when another file with the same name is uploaded it tops the number by 1.
So for example: I already have an image uploaded with filename image.png and I upload another one with the same filename. That file should be renamed to image0.png. When I try to upload another image with filename image.png it should be renamed to image1.png and so on.
I'm not sure how to accomplish this within my code. Can anyone help me out? This is the code snippet that checks for duplicates and appends something to the filename.
if(file_exists("/customers/d/8/e/frankkluytmans.nl/httpd.www/testsite/cms/upload/".$_FILES["image"]["name"]))
{
$filename = explode(".",$_FILES['image']['name']);
$imageName = $filename[0]."hoi.".$filename[1];
}
else
{
$imageName = $_FILES['image']['name'];
}
$image = mysql_real_escape_string(htmlspecialchars("/upload/".$imageName));
if (move_uploaded_file($_FILES["image"]["tmp_name"], "./upload/".$imageName)) {�
mysql_query("INSERT frankkluytmans SET pagid='$pagid', title='$titlename', content='$contentname', image='$image', youtube='$youtube'")
or die(mysql_error());
header("Location: index.php");
}