Gem Posted November 1, 2011 Share Posted November 1, 2011 Hi everybody I am new to arrays, and loops pretty much, so please dont be mean! I am trying to turn a single upload form into a multiple upload form. My logic is I need to use arrays and loop the code for uploading the data from the form to the database (for each of these files, create a thumbnail, resize and rename original, move to folder, create path, and insert all that information into the db) But I have no idea how I've got this in the form (now) .... <pick an album to put the files into> <input type="file" name="image[]" > <input type="file" name="image[]" > and this is the code for the whole upload. It worked for single uploads ... but I'm now at a loss ... The code is abit messy, sorry. I'm fairly sure I need a foreach loop, but I dont know where to put it Really Really hoping you can help. Many thanks in advance <?php ini_set("memory_limit","120M"); include ("DBCONNECT"); include ("CHECK ADMIN"); if ($admin != 1) { header("location:./login.php"); } else { define ("MAX_SIZE","5000"); define ("WIDTH","150"); define ("HEIGHT","100"); function make_thumb($img_name,$filename,$new_w,$new_h) { $ext=getExtension($img_name); if(!strcmp("jpg",$ext) || !strcmp("jpeg",$ext)) $src_img=imagecreatefromjpeg($img_name); if(!strcmp("png",$ext)) $src_img=imagecreatefrompng($img_name); $old_x=imageSX($src_img); $old_y=imageSY($src_img); $ratio1=$old_x/$new_w; $ratio2=$old_y/$new_h; if($ratio1>$ratio2) { $thumb_w=$new_w; $thumb_h=$old_y/$ratio1; } else { $thumb_h=$new_h; $thumb_w=$old_x/$ratio2; } $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h); Imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); if(!strcmp("png",$ext)) imagepng($dst_img,$filename); else imagejpeg($dst_img,$filename); imagedestroy($dst_img); imagedestroy($src_img); } function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } $errors=0; if(isset($_POST['submit1']) && !$errors) { $sqlalbum="INSERT INTO album (album_name) VALUES ('$_POST[newalbumname]')"; mysql_query($sqlalbum) or die ('Error, album_query failed : ' . mysql_error()); } If(isset($_POST['Submit'])) { $image=$_FILES['image']['name']; // if it is not empty if ($image) { $filename = stripslashes($_FILES['image']['name']); $extension = getExtension($filename); $extension = strtolower($extension); if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png")) { echo '<h1>Unknown extension! You can only upload jpg, jpeg and png files</h1>'; $errors=1; } else { $size=getimagesize($_FILES['image']['tmp_name']); $sizekb=filesize($_FILES['image']['tmp_name']); //compare the size with the maxim size we defined and print error if bigger if ($sizekb > MAX_SIZE*1024) { echo '<h1>You have exceeded the size limit!</h1>'; $errors=1; } $image_name=time().'.'.$extension; $newname="uploads/".$image_name; $copied = copy($_FILES['image']['tmp_name'], $newname); if (!$copied) { echo '<h1>Copy unsuccessfull!</h1>'; $errors=1; } else { $thumb_name='uploads/thumbs/thumb_'.$image_name; $thumb=make_thumb($newname,$thumb_name,WIDTH,HEIGHT); }} }} If(isset($_POST['Submit']) && !$errors) { $album = $_POST['album']; $query = "INSERT INTO upload (name, path, album_id, thumbpath ) ". "VALUES ('$image_name', '$newname' , '$album', '$thumb_name')"; mysql_query($query) or die('Error, query failed : ' . mysql_error()); echo "<h1>Thumbnail created Successfully!</h1>"; echo '<img src="'.$thumb_name.'">'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/250201-foreach-loop-array-help/ Share on other sites More sharing options...
PFMaBiSmAd Posted November 1, 2011 Share Posted November 1, 2011 See Example #3 at the following link for how you would loop over the array of uploaded files - http://us.php.net/manual/en/features.file-upload.post-method.php The if(){} statement inside that loop tests if the file was successfully uploaded. You would put (or call) your code to process each file inside that if(){} statement. Quote Link to comment https://forums.phpfreaks.com/topic/250201-foreach-loop-array-help/#findComment-1283872 Share on other sites More sharing options...
Gem Posted November 1, 2011 Author Share Posted November 1, 2011 Thank you, I read it, and tried to implement it, but now the whole thing doesnt work ... all I got is a blank page? I've obviously done it wrong ... can someone please look at this code and if poss. debug it? I've spent an hour looking at it and I cant find the problem (i've commented it to make it easier to read) <?php // database stuff ini_set("memory_limit","120M"); include ("dbconnect"); include ("check admin"); // check user is an admin, if not return to log in screen if ($admin != 1) { header("location:./login.php"); } // if user is an admin, do this ... else { // define a maximum sizes for the uploaded images define ("MAX_SIZE","5000"); define ("WIDTH","150"); define ("HEIGHT","100"); // Get extention function function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } // *********************************************************************** Create the thumnail ******************************************************** function make_thumb($img_name,$filename,$new_w,$new_h) { // get extension $ext=getExtension($img_name); if(!strcmp("jpg",$ext) || !strcmp("jpeg",$ext)) $src_img=imagecreatefromjpeg($img_name); if(!strcmp("png",$ext)) $src_img=imagecreatefrompng($img_name); // gets the dimmensions of the image $old_x=imageSX($src_img); $old_y=imageSY($src_img); // Calculate new dimentions $ratio1=$old_x/$new_w; $ratio2=$old_y/$new_h; if($ratio1>$ratio2) { $thumb_w=$new_w; $thumb_h=$old_y/$ratio1; } else { $thumb_h=$new_h; $thumb_w=$old_x/$ratio2; } // create a new image with the new dimmensions $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h); // resize the original image to the new created one imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); // output the created image to the file. Now we will have the thumbnail into the file named by $filename if(!strcmp("png",$ext)) imagepng($dst_img,$filename); else imagejpeg($dst_img,$filename); //destroys source and destination images. imagedestroy($dst_img); imagedestroy($src_img); } //****************************************************************END create thumbnail ********************************************************* // Error Check! $errors=0; if(isset($_POST['submit1']) && !$errors) { $sqlalbum="INSERT INTO album (album_name) VALUES ('$_POST[newalbumname]')"; mysql_query($sqlalbum) or die ('Error, album_query failed : ' . mysql_error()); } foreach ($_FILES['image']['name']{// ----------------------check the form has been submitted------------------------ if(isset($_POST['Submit'])) { // .................................................................... foreach() ?????? LOOOOOOOOPPPPP START ??? //read the name of the file submitted $image=$_FILES['image']['name']; //...............................array here????? // if it is not empty if ($image) { // get the original name $filename = stripslashes($_FILES['image']['name']); // get the extension of the file in a lower case format $extension = getExtension($filename); $extension = strtolower($extension); // if it is not a jpg, jpeg or png show error and dont upload if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png")) { echo '<h1>Unknown extension! You can only upload jpg, jpeg and png files</h1>'; $errors=1; } // if its ok .... else { // get the size of the image in bytes $size=getimagesize($_FILES['image']['tmp_name']); $sizekb=filesize($_FILES['image']['tmp_name']); //compare the size with the maximum size we defined and print error if bigger if ($sizekb > MAX_SIZE*1024) { echo '<h1>You have exceeded the size limit!</h1>'; $errors=1; } //give the image a unique name $image_name=time().'.'.$extension; //the new name will be containing the full path where will be stored (images folder) $newname="uploads/".$image_name; $copied = copy($_FILES['image']['tmp_name'], $newname); //we verify if the image has been uploaded, and print error instead if (!$copied) { echo '<h1>Copy unsuccessfull!</h1>'; $errors=1; } else { // the new thumbnail image will be placed in images/thumbs/ folder $thumb_name='uploads/thumbs/thumb_'.$image_name; // call the function that will create the thumbnail. The function will get as parameters //the image name, the thumbnail name and the width and height desired for the thumbnail $thumb=make_thumb($newname,$thumb_name,WIDTH,HEIGHT); }} }} //If no errors registred, print the success message and show the thumbnail image created if(isset($_POST['Submit']) && !$errors) { $album = $_POST['album']; $query = "INSERT INTO upload (name, path, album_id, thumbpath ) ". "VALUES ('$image_name', '$newname' , '$album', '$thumb_name')"; mysql_query($query) or die('Error, query failed : ' . mysql_error()); echo "<h1>Thumbnail created Successfully!</h1>"; echo '<img src="'.$thumb_name.'">'; } // END LOOOPPPPPP ???? } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/templ8t.dwt.php" codeOutsideHTMLIsLocked="false" --> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link href="css/layout.css" rel="stylesheet" type="text/css" /> <link href="css/text.css" rel="stylesheet" type="text/css" /> <!-- InstanceBeginEditable name="doctitle" --> <title>Bradley Stoke Judo Club</title> <!-- InstanceEndEditable --> <!-- InstanceBeginEditable name="head" --> <!-- InstanceEndEditable --> <style type="text/css"> <!-- .style1 {color: #FFFFFF} .H2 { font-size: x-large; font-family: Arial, Helvetica, sans-serif; background-color: #FFFF00; } .link {color: #FFFF33} --> </style> </head> <body> <?php echo $errors; ?> <div class="wrapper"> <div class="header"> </div> <div class="body"> <!-- InstanceBeginEditable name="bodytext" --> <center> <span class="pagetitle">Upload New Images</span><br /> <h1> Step 1 </h1> Check that the album you wish to add a image to exists in the list below ( -Albums- ).<br /> If not - add a new album name here... <form name="newalbum" method="post" action=""> <input name="newalbumname" type="text" size="30" maxlength="30" value="<?php echo $newalbumname; ?>" /> <input type="submit" name="submit1" value="Create Album" /> </form> <h1> Step 2 </h1> If the album exists (or you just created one) select it from the list below. Click browse - find the image you want - press upload.<br /> You should see a thumbnail of the image above if it worked. Add another image if necessary <br /> <form name="newad" method="post" enctype="multipart/form-data" action=""> <table> <tr><td><select name="album"> <option value="select" selected="selected" >-Albums-</option> <?php $asql="select * from album order by album_id DESC"; $aresult=mysql_query($asql) or die(mysql_error());; while($arow=mysql_fetch_array($aresult)) { $album_id=$arow['album_id']; $album_name=$arow['album_name']; ?> <option value="<?php echo $album_id; ?>"><?php echo $album_name; ?></option> <?php } ?> </select> <tr><td><input type="file" name="image[]" ></td></tr> <tr><td><input type="file" name="image[]" ></td></tr> <!-- <tr><td><input type="file" name="image[]" ></td></tr> <tr><td><input type="file" name="image[]" ></td></tr> <tr><td><input type="file" name="image[]" ></td></tr> <tr><td><input type="file" name="image[]" ></td></tr> <tr><td><input type="file" name="image[]" ></td></tr> <tr><td><input type="file" name="image[]" ></td></tr> <tr><td><input type="file" name="image[]" ></td></tr> <tr><td><input type="file" name="image[]" ></td></tr> --> <tr><td><input name="Submit" type="submit" value="Upload image"></td></tr> </table> </form> <!-- InstanceEndEditable --> </div> <div class="sidebar"> <!-- MENU --> <BR><?php include "includes/menujs.js"; include "includes/menucss.css"; include "includes/menu.html"; ?> </p> <p> <!-- InstanceBeginEditable name="sidebar2" --> <div class="sidebartitle" align="center"> Quick Links</div> <div class="sidebartext" align="center"> <br /><a href="index.php"><h2>Home</h2></a><br /> <?php if ($user != "y") { $login_text = "Login"; } else { $login_text = "Logout"; } ?> <a href="login.php"><h2><?php echo $login_text; ?></h2></a><br /> <?php if ($user == "y"){ ?> <a href="profile.php?username=<?php echo $username; ?>"><h2>Back to Profile</h2></a><br /> <?php } ?> </div> <div class="sidebartitle" align="center"> Sponser </div> <div class="sidebartext" align="center"> <img src="images/proemblogo.jpg" alt="professionalembroidery" /></div> <!-- InstanceEndEditable --> </p> </div> </div> <div class="clear"></div> <div class="copyright"> <div align="center" class="style1">©Copyright 2010 Bradley Stoke Judo Club || Site Designed by Gem Gale || Site Hosted by <a href="http://www.wotwebsystems.com" class="link">WOT Web Systems</a></div> </div> <?php if ($con) { mysql_close($con);} ?> </body> <!-- InstanceEnd --></html> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/250201-foreach-loop-array-help/#findComment-1283968 Share on other sites More sharing options...
PFMaBiSmAd Posted November 1, 2011 Share Posted November 1, 2011 Your page contains a fatal parse error on line 89 due to a missing ) on line 88. Also, the syntax for a foreach() statement is - http://us3.php.net/manual/en/control-structures.foreach.php You should be developing and debugging php code on a system with error_reporting set to E_ALL and display_errors set to ON in your master php.ini so that php will help you by reporting and displaying all the errors it detects. You will save a TON of time. Quote Link to comment https://forums.phpfreaks.com/topic/250201-foreach-loop-array-help/#findComment-1283980 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.