Need help with multiple upload

Hi…

Okay my attempt is…upload multiple *.gif image at a time…maximum the user can upload 3 image…so the uploading process works okay but when it save the image name in the dtbase it will save like this "userfile.gif userfile.gif userfile.gif"so I don’t know where exactly my mistake…so my question is

1)how to alter the code so that the image name same with the name in upload image folder…eg; sun0.gif,flower1.gif,star0.gif

2)my 2nd question is how to detect if user has upload the same name in the dtbase it’ll echo the error asking the user to rename the img…just for that particular image only…

so I really need help with it…
here’s my currently code…



<?

    $uploaddir = "upimg/";
    $MaxSize = "200000";
    $dbhostname = "localhost";
    $dbusername = "root";
    $dbpassword = "";
    $dbname = "ecard";

   $db = mysql_connect($dbhostname, $dbusername, $dbpassword);
   mysql_select_db($dbname, $db) or die("ERROR: Cannot connect to the database");

    // -----
    function execSQL($strSQL_filenames){
    	if (!$strSQL_filenames == 0){
			$query = "INSERT INTO upimg SET id='NULL', filename='$strSQL_filenames'";
    		#echo "$query";
    		$result = mysql_query($query) or die("ERROR: cannot INSERT data into database, check your permissions");
    	}
    }//function end
    
    #check the number of files selected
    $number_of_files = count($_FILES['userfile']);
    #PUT EACH FILE THROUGH THIS LOOP SEPERATELY
    for($i=0;$i<=$number_of_files;$i++) {
    	#The format you want your new file to have
    	//$filename_format = uniqid(img);
         $filename_format = 'userfile';
   		 if (!$_FILES['userfile']['size'][$i] == 0){
     		if ($_FILES['userfile']['size'][$i] > $MaxSize){
    			echo "ERROR: Whoa! that's a big one alright! ..too big";
   				echo "<br><a href='" . $_SERVER['SCRIPT_NAME'] ."'>Let me try that again..</a>";
   				exit;
    	    }//end if
   			 $tempfile = $_FILES['userfile']['tmp_name'][$i];
   			 $uploadfile = $_FILES['userfile']['name'][$i];
   			 $extension = $_FILES['userfile']['type'][$i];
   			 if (strstr($extension,"jpeg")){
    			$extension=".jpg";
   			 }else if (strstr($extension,"gif")){
    				 $extension=".gif";
    			  }else{
    				echo "ERROR: That type of file is not allowed. only gif/jpeg allowed.";
    				echo "<br><a href='" . $_SERVER['SCRIPT_NAME'] ."'>Let me try that again..</a>";
    				exit;
    			  }
    if(copy($tempfile, $uploaddir.$uploadfile)){
   		 echo "Copy Successfull!<br>";
    }else{
    	echo "ERROR: something happened whilst trying to copy the temp file to the specified folder";
    }
 //  if(rename($uploaddir.$uploadfile,$uploaddir.$filename_format.$extension)){
 //		 echo "The File: " . $_FILES['userfile']['name'][$i] . " was uploaded successfully";
 // 		 echo " and renamed to $filename_format$extension";
 //  }else{
 //	echo "ERROR: Problem renaming the file.. $uploadfile";
 // }
    
    	echo "<p>";
    
    if($i=="0") {
    	$strSQL_filenames = "$filename_format$extension";
    }elseif($i>0){
    	$strSQL_filenames = $strSQL_filenames . " $filename_format$extension";
    	}
    
    }
    if($i==$number_of_files){
    	execSQL($strSQL_filenames);
   	   if($strSQL_filenames) {
    		echo "The following data was entered into the database: \"" . $strSQL_filenames ."\"";
       }
    
    }
}
?>
    <html>
    <head>
    <title>Upload That File!</title>
    </head>
    <body>
    <form name="form1" action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data">
    <input type="file" name="userfile[]" style="width:300;"><br>
    <input type="file" name="userfile[]" style="width:300;"><br>
    <input type="file" name="userfile[]" style="width:300;"><br>
  <br>
    <input type="submit" value="Upload">
    </form>
    </body>
    </html>


tq for ur time guy’s