function rename2($name){ $n = 0; $newname = $name; while(file_exists($newname)){ preg_match("/(\.\w+$)/", $name, $ext); $newname = ereg_replace("$ext[1]$", "($n)$ext[1]", $name); $n++; } return $newname; }