Hey guys. I'm having a bit of trouble here. What I basically want, is to recreate my form X times to suit the needs of the uploader. They choose from a drop-down 1,5,10 and then the form is reproduced x times. I got that.
And so I thought, how am I gonna distinguish between one uploaded photo and another? So I added a counter to the script so that each input was "input_nameX" where X is the number of the count. I also added a hidden value in the form to pass along the number of forms being submitted
So now, in my php upload script, I loop through until my new count reaches the hidden value count. Unfortunately I get the first picture to go through fine, but not subsequent pictures. It will move to the last, and then repeat the last picture over and over until the count is filled.
Not sure I'm doing it right. Should I be adding the items to an array, and then loop through each array item?
Any help would be greatly appreciated.
~Brett
Upload.php Handles the uploads after submission
$max = $_REQUEST['number'];
$maxSize = (1024*1024)*5;
$i = '1';
for($i; $i<=$max; $i++){
$yeari = 'year'.$i;
$cati = 'cat'.$i;
$eventi = 'event'.$i;
$photoi = 'photo'.$i;
$tempname = $_FILES[$photoi]['tmp_name'];
echo "<h2>".$_FILES[$photoi]['name']."</h2>
<p class='article'>";
if(empty($_REQUEST[$yeari]) || $_REQUEST[$yeari] == '' ||
empty($_REQUEST[$cati]) || $_REQUEST[$cati] == '' ||
empty($_REQUEST[$eventi]) || $_REQUEST[$eventi] == ''){
echo Error_Values;
}
if($_FILES[$photoi]['size']>$maxSize){
echo Error_Size;
}
if(is_uploaded_file($tempname)){
$year = $_REQUEST[$yeari];
$cat = $_REQUEST[$cati];
$event = $_REQUEST[$eventi];
$orig_dimensions = getimagesize($tempname);
if($orig_dimensions == false || $orig_dimensions[2] != 2){
die(Error_Image_Type);
}
$orig_width = $orig_dimensions[0];
$orig_height = $orig_dimensions[1];
$image_is_tall = ($orig_height>=$orig_width);
$orig_image = imagecreatefromjpeg($tempname);
$sheight = 75;
$swidth = ceil($orig_width*$sheight/$orig_height);
$lwidth = 500;
$lheight = ceil($orig_height*$lwidth/$orig_width);
if($image_is_tall){
$t = $sheight;
$sheight = $swidth;
$swidth = $t;
$t = $lwidth;
$lwidth = $lheight;
$lheight = $t;
}
$small_image = imagecreatetruecolor($swidth, $sheight);
$large_image = imagecreatetruecolor($lwidth, $lheight);
####
#### CREATE THE DIRECTORIES TO HOLD THE IMAGES
####
$qry = "SELECT a.Year, b.Category, c.Event FROM `Years` a, `Categories` b, `Events` c
WHERE a.yid='".$year."' AND b
.cid='".$cat."' AND
c.eid='".$event."'";
$rslt = mysql_query($qry) or die("<b>".mysql_error()."</b><br />".$qry."<br /><br />");
$_dir = mysql_fetch_array($rslt);
$category = str_replace(" ", "_", $_dir['Category']);
$evt = str_replace(" ", "_", $_dir['Event']);
$_thedir = "/public_html/photographs/".$_dir['Year'].'/'.$category.'/'.$evt.'/thumbs';
define("Root_dir", "/public_html", true);
define("Photo_dir", "/photographs", true);
define("Photo_Path", Root_dir.Photo_dir, true);
define("Year_dir", "/".$_dir['Year'], true);
define("Year_Path", Photo_Path.Year_dir, true);
define("Cat_dir", "/".$category, true);
define("Cat_Path", Year_Path.Cat_dir, true);
define("Event_dir", "/".$evt, true);
define("Event_Path", Cat_Path.Event_dir, true);
define("Thumbs_dir", "/thumbs", true);
define("Thumbs_Path", Event_Path.Thumbs_dir, true);
include("dir_structure.php");
$open_dir = $_SERVER['DOCUMENT_ROOT'].'/photographs/'.$_dir['Year'].'/'.$category.'/'.$evt;
if($handle = opendir($open_dir)){
while(false !== ($file = readdir($handle))){
if(!is_dir($file)){
$lastfile = $file;
}
}
closedir($handle);
}
$nxtfile = $lastfile+1;
$nxtfile = check($nxtfile);
if(imagecopyresampled($small_image, $orig_image, 0, 0, 0, 0, $swidth, $sheight, $orig_width, $orig_height) === FALSE){
echo Error_Create_Thumb;
}
else{
$new_small = "/home/ridgeswi/".Thumbs_Path."/".$nxtfile.".jpg";
if(@imagejpeg($small_image, $new_small, 100) === false){
$small_created = "<font style=\"color: #f00;\"><b>Missing.</b> Failed to create.</font>";
$small_create = false;
echo Error_Create_New_Thumb;
}
else{
$thumb = "http://www.ridgeswimclub.org/photographs/".$_dir['Year'].'/'.$category.'/'.$evt."/thumbs/".$nxtfile.".jpg";
$small_created = "<font style=\"color: #090;\">Created successfully.</font>";
$small_create = true;
echo New_Thumb_Success;
}
}
if(imagecopyresampled($large_image, $orig_image, 0, 0, 0, 0, $lwidth, $lheight, $orig_width, $orig_height) === FALSE){
echo Error_Create_Large;
}
else{
$new_large = "/home/ridgeswi/".Event_Path."/".$nxtfile.".jpg";
if(@imagejpeg($large_image, $new_large, 100) === false){
$large_created = "<font style=\"color: #f00;\"><b>Missing.</b> Failed to create.</font>";
$large_create = false;
echo Error_Create_New_Image;
}
else{
$photo = "http://www.ridgeswimclub.org/photographs/".$_dir['Year'].'/'.$category.'/'.$evt."/".$nxtfile.".jpg";
$large_created = "<font style=\"color: #090;\">Created successfully.</font>";
$large_create = true;
echo New_Image_Success;
}
}
if($small_create === true && $large_create === true){
$q1 = "INSERT INTO `Photos` (pyid, pcid, peid, Thumbnail, Photograph) VALUES ('$year', '$cat', '$event', '$thumb', '$photo')";
$r1 = mysql_query($q1);
if(!$r1 || $r1 === FALSE){
echo Error_Database;
}
else{
echo "<br /><font style='color: #090;'><b><i>Success!!</i></b></font>";
}
}
else{
echo "<br /><font style='color: #000;'><i>Query not run. One of the images wasn't created:</i><br /><b>Thumbnail:</b> "
.$small_created."<br /><b>Large Photo:</b> ".$large_created."</font>";
}
}
echo "</p>";
}