tahakirmani Posted August 12, 2013 Share Posted August 12, 2013 I am trying to upload multiple files from HTML form to a folder and storing information in my database. I have written the following code, but it is not working correctly. If I upload 1 image and left the other two Image fields,then it stores 3 images of same name and same Image and if I upload 3 different Images in three difference fields then it takes only the last image and replace others with it, and shows the same image for all. In Simple the problem is that it is still uploading just 1 file, but now its entring data 3 times in database. Kindly guide me. HTML Image Fields <file> <Image2> <Image3> /*------------------- IMAGE QUERY ---------------*/ $allowedExts = array("gif", "jpeg", "jpg", "png"); $extension = end(explode(".", $_FILES["file"]["name"])); if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["image2"]["type"] == "image/gif") || ($_FILES["image3"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["image2"]["type"] == "image/jpeg") || ($_FILES["image3"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["image2"]["type"] == "image/jpg") || ($_FILES["image3"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/pjpeg") || ($_FILES["image2"]["type"] == "image/pjpeg") || ($_FILES["image3"]["type"] == "image/pjpeg") || ($_FILES["file"]["type"] == "image/x-png") || ($_FILES["image2"]["type"] == "image/x-png") || ($_FILES["image3"]["type"] == "image/x-png") || ($_FILES["file"]["type"] == "image/png") || ($_FILES["image2"]["type"] == "image/png") || ($_FILES["image3"]["type"] == "image/png")) //&& ($_FILES["file"]["size"] < 200000) && in_array($extension, $allowedExts)) { if ($_FILES["file"]["error"] > 0 && $_FILES["image"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br>"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br>"; echo "Type: " . $_FILES["file"]["type"] . "<br>"; echo "Size: " . ($_FILES["file"]["size"] / 200000) . " kB<br>"; echo "Upload: " . $_FILES["image2"]["name"] . "<br>"; echo "Type: " . $_FILES["image2"]["type"] . "<br>"; echo "Size: " . ($_FILES["image2"]["size"] / 200000) . " kB<br>"; $image_name= $_FILES["file"]["name"]; $random_name= rand().$_FILES["file"]["name"]; $image_name2= $_FILES["image2"]["name"]; $random_name2= rand().$_FILES["image2"]["name"]; $image_name3= $_FILES["image3"]["name"]; $random_name3= rand().$_FILES["image3"]["name"]; $path= move_uploaded_file($_FILES["file"]["tmp_name"], "upload/products/" . $random_name); $path2= move_uploaded_file($_FILES["image2"]["tmp_name"], "upload/products/" . $random_name); $path3= move_uploaded_file($_FILES["image3"]["tmp_name"], "upload/products/" . $random_name); $folder="upload/products/" .$random_name; $folder2="upload/products/" .$random_name; $folder3="upload/products/" .$random_name; //echo "Stored in: " . "upload/" .rand(). $_FILES["file"]["name"]; echo "Stored in: "."upload/products/". $random_name; /* $sql = "INSERT INTO `category_images` (`image_name`,`image_location`) VALUES ('".$image_name."', '".$path."')"; */ $sql = "Insert into product_images (product_id,name,images) VALUES ($current_id,'$image_name', '$folder')"; $sql2 = "Insert into product_images (product_id,name,images) VALUES ($current_id,'$image_name2', '$folder2')"; $sql3 = "Insert into product_images (product_id,name,images) VALUES ($current_id,'$image_name3', '$folder3')"; $result = mysql_query($sql); mysql_query($sql2); mysql_query($sql3); if ($result) { echo "successfull"; } else { echo mysql_error(); } } } else { echo "Invalid file"; } /*----------------- IMAGE QUERY END ------------------*/ Link to comment https://forums.phpfreaks.com/topic/281084-how-to-upload-multiple-images-from-html-from-to-files/ Share on other sites More sharing options...
mac_gyver Posted August 12, 2013 Share Posted August 12, 2013 i recommend starting with the example form and form processing code in Example #3 in the php.net documentation - http://www.php.net/manual/en/features.file-upload.post-method.php this uses an array for the form fields and uses a loop to eliminate all the repetitive code. the if(){} conditional statement in that code will only be true if an image was successfully upload. Link to comment https://forums.phpfreaks.com/topic/281084-how-to-upload-multiple-images-from-html-from-to-files/#findComment-1444564 Share on other sites More sharing options...
fastsol Posted August 12, 2013 Share Posted August 12, 2013 Check this tutorial out http://www.youtube.com/playlist?list=PL7413DDD1EB6A14BD Link to comment https://forums.phpfreaks.com/topic/281084-how-to-upload-multiple-images-from-html-from-to-files/#findComment-1444574 Share on other sites More sharing options...
tahakirmani Posted August 13, 2013 Author Share Posted August 13, 2013 Thank you so much for the guidance and help. I have watched the video and checked the other tutorial too. Now I have written the following code and its giving me a new error message. I Have updated my code. Kindly check it and guide me. Thanks. Invalid file Link to comment https://forums.phpfreaks.com/topic/281084-how-to-upload-multiple-images-from-html-from-to-files/#findComment-1444702 Share on other sites More sharing options...
tahakirmani Posted August 13, 2013 Author Share Posted August 13, 2013 /*-------------------IMAGE QUERY ---------------*/ $allowedExts = array("gif", "jpeg", "jpg", "png"); $extension = end(explode(".", $_FILES["files"]["name"])); if ((($_FILES["files"]["type"] == "image/gif") || ($_FILES["files"]["type"] == "image/jpeg") || ($_FILES["files"]["type"] == "image/jpg") || ($_FILES["files"]["type"] == "image/pjpeg") || ($_FILES["files"]["type"] == "image/x-png") || ($_FILES["files"]["type"] == "image/png")) //&& ($_FILES["file"]["size"] < 200000) && in_array($extension, $allowedExts)) { if ($_FILES["files"]["error"] > 0 ) { echo "Return Code: " . $_FILES["files"]["error"] . "<br>"; } else { echo "Upload: " . $_FILES["files"]["name"] . "<br>"; echo "Type: " . $_FILES["files"]["type"] . "<br>"; echo "Size: " . ($_FILES["files"]["size"] / 200000) . " kB<br>"; $image_name= $_FILES["files"]["name"]; $random_name= rand().$_FILES["files"]["name"]; $path= move_uploaded_file($_FILES["files"]["tmp_name"], "upload/products/" . $random_name); /* $path2= move_uploaded_file($_FILES["image2"]["tmp_name"], "upload/products/" . $random_name); $path3= move_uploaded_file($_FILES["image3"]["tmp_name"], "upload/products/" . $random_name); */ $folder="upload/products/" .$random_name; // $folder2="upload/products/" .$random_name; // $folder3="upload/products/" .$random_name; //echo "Stored in: " . "upload/" .rand(). $_FILES["file"]["name"]; echo "Stored in: "."upload/products/". $random_name; /* $sql = "INSERT INTO `category_images` (`image_name`,`image_location`) VALUES ('".$image_name."', '".$path."')"; */ $sql = "Insert into product_images (product_id,name,images) VALUES ($current_id,'$image_name', '$folder')"; $sql2 = "Insert into product_images (product_id,name,images) VALUES ($current_id,'$image_name2', '$folder2')"; $sql3 = "Insert into product_images (product_id,name,images) VALUES ($current_id,'$image_name3', '$folder3')"; $result = mysql_query($sql); //mysql_query($sql2); //mysql_query($sql3); if ($result) { echo "successfull"; } else { echo mysql_error(); } } } else { echo "Invalid file"; } /*----------------- IMAGE QUERY END ------------------*/ Link to comment https://forums.phpfreaks.com/topic/281084-how-to-upload-multiple-images-from-html-from-to-files/#findComment-1444703 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.