justAnoob Posted May 4, 2009 Share Posted May 4, 2009 this script works great for uploading 1 picture into my server directory and the image path to mysql. As you can see below my filefield name is "image" for the one picture that can be uploaded. If I added 4 more fields.... "image2" "image3" "image4" "image5" to my form... Would it be complicated to just add the upload of these fields into the script???? The first image is required when you upload,,,, but the other 4 will not be required. I'm kinda confused,, other people tell me to just start with a new script... But I like the one I have. <?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")) { echo '<h1>Picture #1 is not in correct format.</h1>'; $errors=1; } else { $size=filesize($_FILES['image']['tmp_name']); $size2=filesize($_FILES['image2']['tmp_name2']); if ($size || $size2 > MAX_SIZE*1024) { echo '<h1>You have exceeded the size limit on one of your pictures.</h1>'; $errors=1; } $category = $_POST['listmenu']; $image_name=time().'.'.$extension; $newname="userimages/$category/".$image_name; $copied = copy($_FILES['image']['tmp_name'], $newname); if (!$copied) { echo '<h1>File #1 unsuccessfull!</h1>'; $errors=1; } } } } $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 member_trades(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) { header ("http://www.xxxxxxxxxxx.com/previewsave.php"); echo "<h1>Image Uploaded Successfully!</h1>"; echo '<img src="' . $newname . '" width="150" border="0"><br />'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/156721-want-to-keep-this-script-but-just-add-for-extra-image-upload/ Share on other sites More sharing options...
justAnoob Posted May 4, 2009 Author Share Posted May 4, 2009 I tried doing something like this..... if(!empty(image2)) { run the upload script again just changing the variables to work with the filefield name "image2" } if(!empty(image3)) { run the upload script etc........ } But i think I ran into the problem where everything gets inserted into MySQL Here is the line originally with just having 1 upload box.. <?php $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 xxxxxxxxx(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());?> Then when I add the other image2, image3, image4 and image5 filefield boxes for optional upload...that would just create a ton variables and a big mess.?..... Could I do a loop of some sort??? I'm not that good with loops. Quote Link to comment https://forums.phpfreaks.com/topic/156721-want-to-keep-this-script-but-just-add-for-extra-image-upload/#findComment-826164 Share on other sites More sharing options...
justAnoob Posted May 4, 2009 Author Share Posted May 4, 2009 Here is a correction of the original script that I'm working with... <?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")) { echo '<h1>Picture is not in correct format.</h1>'; $errors=1; } else { $size=filesize($_FILES['image']['tmp_name']); if ($size > MAX_SIZE*1024) { echo '<h1>You have exceeded the size limit on one of your pictures.</h1>'; $errors=1; } $category = $_POST['listmenu']; $image_name=time().'.'.$extension; $newname="userimages/$category/".$image_name; $copied = copy($_FILES['image']['tmp_name'], $newname); if (!$copied) { echo '<h1>File #1 unsuccessfull!</h1>'; $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 xxxxxxxxx(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) { header ("http://www.xxxxxxxxxx.com/previewsave.php"); echo "<h1>Image Uploaded Successfully!</h1>"; echo '<img src="' . $newname . '" width="150" border="0"><br />'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/156721-want-to-keep-this-script-but-just-add-for-extra-image-upload/#findComment-826171 Share on other sites More sharing options...
mikesta707 Posted May 4, 2009 Share Posted May 4, 2009 the easiest thing to do would be to make a function that uploads something, and just call that function mulitple times. Honestly, your script is perfectly fine, just put it a function, and you will be good to go Quote Link to comment https://forums.phpfreaks.com/topic/156721-want-to-keep-this-script-but-just-add-for-extra-image-upload/#findComment-826207 Share on other sites More sharing options...
justAnoob Posted May 4, 2009 Author Share Posted May 4, 2009 but wouldn't the script change if I had 5 different filefields for the uploads????? image image2 image3 and so on. Quote Link to comment https://forums.phpfreaks.com/topic/156721-want-to-keep-this-script-but-just-add-for-extra-image-upload/#findComment-826214 Share on other sites More sharing options...
justAnoob Posted May 4, 2009 Author Share Posted May 4, 2009 I tried doing the functin for each upload,, but couldn't get it to work.... Any suggestions or some code snippets? Quote Link to comment https://forums.phpfreaks.com/topic/156721-want-to-keep-this-script-but-just-add-for-extra-image-upload/#findComment-826293 Share on other sites More sharing options...
haris244808 Posted July 22, 2011 Share Posted July 22, 2011 Can you pls send me the SQL code, Php and html code because i also want to add this kind of forms to my site... THanks Quote Link to comment https://forums.phpfreaks.com/topic/156721-want-to-keep-this-script-but-just-add-for-extra-image-upload/#findComment-1246089 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.