justAnoob Posted May 14, 2009 Share Posted May 14, 2009 Hi,,, the image upload script that I'm using works fantastic(I think) for 1 image..Now lets say I have 5 file field boxes. What would be the easiest way to loop through all 5 boxes for upload?? Can someone get me started? <?php session_start(); include "connection.php"; mysql_connect("$host", "$username", "$password") or die("Could not connect."); mysql_select_db("$db_name") or die("Could not find database"); $item_name = mysql_real_escape_string($_POST['item_name']); $description = mysql_real_escape_string($_POST['description']); $in_return = mysql_real_escape_string($_POST['in_return']); define ("MAX_SIZE","1000"); 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['submit'])) { $image=$_FILES['image']['name']; if($image) { $filename = stripslashes($_FILES['image']['name']); $extension = getExtension($filename); $extension = strtolower($extension); if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "gif") && ($extension != "png")) { $_SESSION['badformat'] = "Your picture must be a .JPG .GIF or .PNG"; header("location: http://www.---------.com/---------.php"); exit(); $errors=1; } else { $size=filesize($_FILES['image']['tmp_name']); if ($size > MAX_SIZE*1024) { $_SESSION['toobig'] = "Your picture can not exceed 1 megabyte."; header("location: http://www.---------.com/---------.php"); exit(); $errors=1; } $category = $_POST['listmenu']; $image_name=time().'.'.$extension; $newname="userimages/$category/".$image_name; $copied = copy($_FILES['image']['tmp_name'], $newname); if (!$copied) { $_SESSION['notcopy'] = "There was an error posting your trade. Please try again later."; header("location: http://www.-------.com/--------.php"); exit(); $errors=1; } } } } // if everything is good, post new item for the user $mysqlcategory = $category; $imgpath = $newname; $findit = $_SESSION['id']; $result=mysql_query("SELECT id FROM members WHERE username = '$findit'"); $row=mysql_fetch_assoc($result); $user_id = $row['id']; $sql = "INSERT INTO abcxyz(item_name, description, in_return, imgpath, category, user_id)VALUES('$item_name','$description','$in_return', '$imgpath', '$mysqlcategory', '$user_id')"; mysql_query($sql) or die(mysql_error()); // go to confirmation page if upload is completed. if(isset($_POST['submit']) && !$errors) { $_SESSION['picload'] = $newname; $_SESSION['picname'] = $item_name; header("location: http://www.-----------.com/---------.php"); exit(); } ?> Also,, when I add the extra file fields to my form,, should they be named the same,, or different?? I was thinking the same name so the script can cycle through all the file fields. Maybe with a Foreach statement?? Quote Link to comment Share on other sites More sharing options...
jackpf Posted May 14, 2009 Share Posted May 14, 2009 You could use foreach($_FILES as $key => $value) { //your processing } You'll have to refer to $_FILES['filename'] as $_FILES[$key] though. Quote Link to comment Share on other sites More sharing options...
justAnoob Posted May 14, 2009 Author Share Posted May 14, 2009 Could you explain a tad bit more ?? Quote Link to comment Share on other sites More sharing options...
jackpf Posted May 15, 2009 Share Posted May 15, 2009 Ok, $_FILES as an array. For each file uploaded, that filename (name of the form element) is put into the array. So, if you use foreach(), you can go through each file, and do whatever you want with it. Quote Link to comment Share on other sites More sharing options...
justAnoob Posted May 15, 2009 Author Share Posted May 15, 2009 I'm still confused,, I'll just play around with it. And look for some examples.. Quote Link to comment Share on other sites More sharing options...
jackpf Posted May 15, 2009 Share Posted May 15, 2009 What are you confused with? Quote Link to comment 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.