Code: Select all
//begin image upload form
echo '
<br /><br />
<form method="post" action="'.$_SERVER['PHP_SELF'].'" enctype="multipart/form-data" id="imagesForm">
<fieldset>
<legend>
Upload your images:
</legend>
<label>
Image 1:
<input type="file" name="image1" />
</label>
<br />
<label>
Image 2:
<input type="file" name="image2" />
</label>
<br />
<label>
Image 3:
<input type="file" name="image3" />
</label>
<br />
<label>
Image 4:
<input type="file" name="image4" />
</label>
<br />
<label>
Image 5:
<input type="file" name="image5" />
</label>
<br />
<br />
<input type="submit" name="imageSubmit" value="Upload the images>>" />
</fieldset>
</form>
';
$imageSubmitBtn=$_POST['imageSubmit'];
//start the processing for the image upload form
if(isset($imageSubmitBtn)){
//assign the various values required
//check if any of the fields has values set to them, and if they do, then start the image control function
if(isset($_FILES['image1']['type'])){
imgControl($_FILES['image1'], $uniqId, $userName);
}
if(isset($_FILES['image2']['type'])){
imgControl($_FILES['image2'], $uniqId, $userName);
}
if(isset($_FILES['image3']['type'])){
imgControl($_FILES['image3'], $uniqId, $userName);
}
if(isset($_FILES['image4']['type'])){
imgControl($_FILES['image4'], $uniqId, $userName);
}
if(isset($_FILES['image5']['type'])){
imgControl($_FILES['image5'], $uniqId, $userName);
}
}
//function that handles images, takes as argument the array $_FILES['name_of_image'] and the unique id of the concerned user
function imgControl($imgHolder, $uniqUserId, $userName){
//get time right now
$time=microtime();
//create a global variable that gives the name of the image
global $imgAssignableName;
//get the type of the image
$imgType=$imgHolder['type'];
//get the current name of the image
$imgActualName=$imgHolder['name'];
//translate into the type of extension that browsers understand
$imgType=str_replace('/','',$imgType);
$imgType=str_replace('image','',$imgType);
$imgType=str_replace('pjpeg','jpeg',$imgType);
$imgType=str_replace('x-png','png',$imgType);
//make the new name of the image by adding together the username, and the unique user id and the name of the image
$imgNewName=$uniqUserId.$time.'.'.$imgType;
//make the assignable name by adding the path to the new name
$imgAssignable='uploadedImages/'.$imgNewName.'';
//copy the image onto the server
copy($imgHolder['tmp_name'], $imgAssignable);
//insert a record in the database telling the username and the image name
$query1=@mysql_query("insert into userimages set id='', userName='$userName', imageName='$imgAssignable', originalName='$imgActualName'");
//if query fails, tell me why
if(!$query1){
echo 'Oops there\'s been an error, please try again later. Your image has not been uploaded.<br />';
echo mysql_error();
unlink($imgAssignable);
}
}
But what's happening is, the function is being executed for all of the form fields, regardless of the if(isset()) check. So redundant values are getting inserted into the database... what's going wrong? Please help me...