if ($_FILE['uploaded_file']['size'] > $max_size)
echo "<b>File too large to upload</b>";
Put that as the one of the conditions over there. How come its hard for you when you can code so much.
Also, for checking if the imagename is already in the database, use the SELECT query with the name of file you are uploading and if the mysql_num_rows returns value more than 0, you know the imagename is already present in the database.