<?php
function upload($upload, $save_dir) //$upload is the $_FILES["uploadfile"], $save_dir is the directory to store the uploaded file
{
	$upload_name = $upload["name"];
	$upload_type = $upload["type"];
	$upload_tmp_name = $upload["tmp_name"];
	$upload_size = $upload["size"];
	
	if ($upload_tmp_name != '' && $upload_tmp_name != 'none')
	{
		$extension = strrchr($upload_name, ".");
				
		if ($extension == '' || ! isset($extension))
		{
			$extension = '';
			$base = $upload_name;
		}
		else
		{
			$pos = strrpos($upload_name, ".");
			$base = substr($upload_name, 0, $pos);
		}
		
		$n = 1;
		
		while (file_exists($save_dir . $base . $copy . $extension))
		{
			$copy = "_copy" . $n;
			$n++;
		}
		
		$new_name = $base . $copy . $extension;
		
		$dest = $save_dir . $base . $copy . $extension;

		if (copy($upload_tmp_name, $dest))
		{
			return $new_name;
		}
	}
	return -1;
}
?>