In many situations, we need to make the filename different on the server when creating them to prevent duplication. And the most common answer to that seems to be, append th
Here's a simple function I wrote for this purpose:
function incrementFileName($file_path,$filename){
if(count(glob($file_path.$filename))>0)
{
$file_ext = end(explode(".", $filename));
$file_name = str_replace(('.'.$file_ext),"",$filename);
$newfilename = $file_name.'_'.count(glob($file_path."$file_name*.$file_ext")).'.'.$file_ext;
return $newfilename;
}
else
{
return $filename;
}
}
USAGE:
$newName = incrementFileName( "uploads/", $_FILES["my_file"]["name"] );
move_uploaded_file($_FILES["my_file"]["tmp_name"],"uploads/".$newName);