Hi, I need to have a form with multiple image and description fields, say 15. So if 15 are filled in then it adds then it adds the images to 2 dirs(thumb and images) and saves the image pathname and the descriptions to the 2 mysql tables.
I have this working ok if I dont have multiple fields i my form, but I'd really like to have multiple fields!
This is the code I have so far.
Form
<form class="form1" action="pete_submit.php" method="post" enctype="multipart/form-data" name="image_upload_form" id="image_upload_form" style="margin-bottom:0px;">
<input type="file" id="image_upload_box" size="30" name="image_upload_box" />
<span class="text2">Description</span> <input type=text name="adescription"><br />
<br />
<input type="submit" name="submit" value="Upload Images" />
<input name="submitted_form" type="hidden" id="submitted_form" value="image_upload_form" />
</form>
pete_submit.php (this also resizes the images twice for thumbs and resized full images)
<?php
ini_set("memory_limit", "400000000"); // for large images so that we do not get "Allowed memory exhausted"
include '../inc/connect.php';
// upload the file
if ((isset($_POST["submitted_form"])) && ($_POST["submitted_form"] == "image_upload_form")){
// file needs to be jpg,gif,bmp,x-png and 4 MB max
if (($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg" || $_FILES["image_upload_box"]["type"] == "image/gif" || $_FILES["image_upload_box"]["type"] == "image/x-png") && ($_FILES["image_upload_box"]["size"] < 8000000)){
// some settings
$max_upload_width = 500;
$max_upload_height = 500;
$thumb_height = 150;
$thumb_width = 150;
$message = 'Use the browsers back button to try again, or try loading a different image.';
// if uploaded image was JPG/JPEG
if($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg"){
$image_source = imagecreatefromjpeg($_FILES["image_upload_box"]["tmp_name"]);
}
// if uploaded image was GIF
if($_FILES["image_upload_box"]["type"] == "image/gif"){
$image_source = imagecreatefromgif($_FILES["image_upload_box"]["tmp_name"]);
}
// if uploaded image was PNG
if($_FILES["image_upload_box"]["type"] == "image/x-png"){
$image_source = imagecreatefrompng($_FILES["image_upload_box"]["tmp_name"]);
}
$remote_file = "../images/galleryimages/".$_FILES["image_upload_box"]["name"];
$thumb_file = "../images/gallerythumbs/".$_FILES["image_upload_box"]["name"];
imagejpeg($image_source,$remote_file,100);
chmod($remote_file,0644);
//Save image as thumb
// get width and height of original image
list($image_width, $image_height) = getimagesize($remote_file);
if($image_width>$thumb_width || $image_height>$thumb_height){
$proportions = $image_height/$image_width;
$new_width = $thumb_width;
$new_height = ($thumb_width*$proportions);
$thumb_image = imagecreatetruecolor($new_width , $new_height);
$image_source = imagecreatefromjpeg($remote_file);
imagecopyresampled($thumb_image, $image_source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);
imagejpeg($thumb_image,$thumb_file,100);
imagedestroy($thumb_image);
//add the files pathname to the database table thumbs_gallery
$adescription = $_POST['adescription'];
$query = "INSERT INTO gallerythumbs VALUE('', '$adescription', '$thumb_file', '1')";
$result = mysql_query($query);
if(!$result){
$error = 'An error occured: '. mysql_error().'<br />';
$error.= 'Query was: '.$query.'<br />';
die($message);
}
$query = "SELECT `id` FROM `gallerythumbs` WHERE `filename` LIKE '$thumb_file'";
$result = mysql_query($query);
if(!$result) {
$error = 'An error occured: '. mysql_error().'<br />';
$error.= 'Query was: '.$query;
die($message);
}
$q2 = mysql_fetch_assoc($result);
mysql_free_result($result);
$id = $q2['id'];
}
// get width and height of original image
//if landscape make width 500, if portrait make height 500
list($image_width, $image_height) = getimagesize($remote_file);
if($image_width>$max_upload_width || $image_height >$max_upload_height){
$proportions = $image_width/$image_height;
if($image_width>$image_height){
$new_width = $max_upload_width;
$new_height = round($max_upload_width/$proportions);
}
else{
$new_height = $max_upload_height;
$new_width = round($max_upload_height*$proportions);
}
}
$new_image = imagecreatetruecolor($new_width , $new_height);
$image_source = imagecreatefromjpeg($remote_file);
imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);
imagejpeg($new_image,$remote_file,100);
//add the files pathname to the database table image_gallery.
$adescription = $_POST['adescription'];
$query = "INSERT INTO galleryimages VALUE($id, '$adescription', '$remote_file', '1')";
$result = mysql_query($query);
if(!$result){
$error = 'An error occured: '. mysql_error().'<br />';
$error.= 'Query was: '.$query;
die($message);
}
imagedestroy($image_source);
header("Location: peteadd.php");
exit;
}
else{
header("Location: peteadd.php");
exit;
}
}
?>
Can anyone help me out on this?
Thanks