The answer that gave @ A.Cedano is excellent to leave. I would advise that always assume that the file already exists, and therefore you have to rename it.
The problem is that if the file mi_imagen.png
already exists, you can not just put mi_imagen2.png
because first you have to check if mi_imagen2.png
exists.
The solution in these cases is to assign a name that can not be repeated. For example, using the timestamp.
$name = basename($_FILES["imagenes"]["name"][$key]);
list($base,$extension) = explode('.',$name);
$newname = implode('.', [$base, time(), $extension];
move_uploaded_file($tmp_name, "$uploads_dir/$newname");
And in that case your files would be called as mi_imagen.1489403902.png
.
According to the proposed solution, the only thing that you would need is to contemplate the case that the name of the file contains a point in between, but that remains as a task for you.
Better than the timestamp would be to use a UUID generator, but the idea is not to complicate things so much.