Funny, here's a function that I recently wrote:
function generateFileName($strPath, $strFileName)
{
$strNewFileName = $strFileName;
$iIndex = 1;
while(file_exists($strPath.$strNewFileName))
{
$strSuffix = sprintf("%04s", $iIndex);
$strNewFileName =
preg_replace("/(.*).(.+$)/", "\1_".$strSuffix.".\2", $strFileName);
$iIndex++;
}
return $strNewFileName;
}
supply it with the path the file will go in, as well as the desired filename. if it exists, the function will generate a filename that is unique.
i.e. you upload myfile.txt and there is already a file by that name, this function will return myfile_0001.txt
p.