There maybe an easier way firstly you will need to check if the filename on the server already exists if so create a random file name making sure you keep the file extension the same.
//cut out of one of my scripts
$getFileExt=explode(".",$_FILES['uploadfile']['name']);
$numDot=count($getFileExt);
$_FILES['uploadfile']['name'] = substr(md5(uniqid(mktime())),0,15).".".$getFileExt[($numDot-1)];
As you can see the $_FILES['uploadfile']['name'] stays the same so no need to use extra variables.
Basically the file name wil be 15characters long by using the mktime() The Current Time in a Unix Time Stamp than uses uniqid basically doesnt do much in this script just makes sure it doesnt go all weird. Then the File name is hased you can get rid of the md5() if you rather just see numbers. But to be truly random that just helps.
$getFileExt[($numDot-1)];
That just gets the extension which is checking to make sure there are no extra . within the file name and gets the very last bit of information of the last . this would be the filename.
Hope this helps