haris244808 Posted September 28, 2011 Share Posted September 28, 2011 hi there how can i modify this code to let me upload multiple images in database? thanks / Start a session for error reporting session_start(); //Connect to database require_once('../Connections/connect_to_mysql.php'); //check if the button is pressed if(isset($_POST['submit'])){ // Check to see if the type of file uploaded is a valid image type function is_valid_type($file) { // This is an array that holds all the valid image MIME types $valid_types = array("image/jpg", "image/jpeg", "image/bmp", "image/gif", "image/png"); if (in_array($file['type'], $valid_types)) return 1; return 0; } // This variable is the path to the image folder where all the images are going to be stored // Note that there is a trailing forward slash $TARGET_PATH = "adminform_images/"; // Get our POSTed variables $title = $_POST['title']; $name = $_POST['name']; $surname = $_POST['surname']; $email = $_POST['email']; $phone = $_POST['phone']; $city = $_POST['city']; $comments = $_POST['comments']; $image = $_FILES['image']; $price = $_POST['price']; // Sanitize our inputs $title = mysql_real_escape_string($title); $name = mysql_real_escape_string($name); $surname = mysql_real_escape_string($surname); $email = mysql_real_escape_string($email); $phone = mysql_real_escape_string($phone); $city = mysql_real_escape_string($city); $comments = mysql_real_escape_string($comments); $image['name'] = mysql_real_escape_string($image['name']); $price = mysql_real_escape_string($price); // Build our target path full string. This is where the file will be moved do // i.e. images/picture.jpg $TARGET_PATH .= $image['name']; // Make sure all the fields from the form have inputs if ( $title == "" || $name == "" || $email == "" || $city == "" || $comments == "" || $price == ""|| $image['name'] == "" ) { echo "Te gjithe fushat duhet plotesuar. <a href=login.php>Provo perseri</a>"; exit; } // Check to make sure that our file is actually an image // You check the file type instead of the extension because the extension can easily be faked if (!is_valid_type($image)) { echo "Fotografite duhet te jene te formateve jpg, jpeg, bmp, gif, ose png. <a href=login.php>Kthehu prapa</a>"; exit; } // Here we check to see if a file with that name already exists // You could get past filename problems by appending a timestamp to the filename and then continuing if (file_exists($TARGET_PATH)) { echo "Ju lutem nderroni emrin e fotos dhe provoni perseri. <a href=login.php>Kthehu prapa</a>"; exit; } // Lets attempt to move the file from its temporary directory to its new home if (move_uploaded_file($image['tmp_name'], $TARGET_PATH)) { // NOTE: This is where a lot of people make mistakes. // We are *not* putting the image into the database; we are putting a reference to the file's location on the server $sql = "INSERT INTO admins_form (title, name, surname, email, phone, city, comments, price, date_added, filename) values ('$title', '$name', '$surname', '$email', '$phone', '$city', '$comments', '$price', now(), '" . $image['name'] . "')"; $result = mysql_query($sql) or die ("Futja e te dhenave ne databaze DESHTOI. <a href=login.php>Provo perseri</a> "); echo "Shtimi i te dhenave u krye me SUKSES. Klikoni per te shkuar tek <a href=../index.php>faqja kryesore</a>"; exit; } else { // A common cause of file moving failures is because of bad permissions on the directory attempting to be written to // Make sure you chmod the directory to be writeable echo "Shtimi i te dhenave NUK u krye me Sukses. Ju lutem provoni <a href=login.php>perseri</a>"; exit; } } Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted September 28, 2011 Share Posted September 28, 2011 where is your form here? the short sweet way to allow for multiple image upload is to have multiple file type inputs in a form.. Quote Link to comment Share on other sites More sharing options...
haris244808 Posted September 28, 2011 Author Share Posted September 28, 2011 hi there how can i modify this code to let me upload multiple images in database? thanks / Start a session for error reporting session_start(); //Connect to database require_once('../Connections/connect_to_mysql.php'); //check if the button is pressed if(isset($_POST['submit'])){ // Check to see if the type of file uploaded is a valid image type function is_valid_type($file) { // This is an array that holds all the valid image MIME types $valid_types = array("image/jpg", "image/jpeg", "image/bmp", "image/gif", "image/png"); if (in_array($file['type'], $valid_types)) return 1; return 0; } // This variable is the path to the image folder where all the images are going to be stored // Note that there is a trailing forward slash $TARGET_PATH = "adminform_images/"; // Get our POSTed variables $title = $_POST['title']; $name = $_POST['name']; $surname = $_POST['surname']; $email = $_POST['email']; $phone = $_POST['phone']; $city = $_POST['city']; $comments = $_POST['comments']; $image = $_FILES['image']; $price = $_POST['price']; // Sanitize our inputs $title = mysql_real_escape_string($title); $name = mysql_real_escape_string($name); $surname = mysql_real_escape_string($surname); $email = mysql_real_escape_string($email); $phone = mysql_real_escape_string($phone); $city = mysql_real_escape_string($city); $comments = mysql_real_escape_string($comments); $image['name'] = mysql_real_escape_string($image['name']); $price = mysql_real_escape_string($price); // Build our target path full string. This is where the file will be moved do // i.e. images/picture.jpg $TARGET_PATH .= $image['name']; // Make sure all the fields from the form have inputs if ( $title == "" || $name == "" || $email == "" || $city == "" || $comments == "" || $price == ""|| $image['name'] == "" ) { echo "Te gjithe fushat duhet plotesuar. <a href=login.php>Provo perseri</a>"; exit; } // Check to make sure that our file is actually an image // You check the file type instead of the extension because the extension can easily be faked if (!is_valid_type($image)) { echo "Fotografite duhet te jene te formateve jpg, jpeg, bmp, gif, ose png. <a href=login.php>Kthehu prapa</a>"; exit; } // Here we check to see if a file with that name already exists // You could get past filename problems by appending a timestamp to the filename and then continuing if (file_exists($TARGET_PATH)) { echo "Ju lutem nderroni emrin e fotos dhe provoni perseri. <a href=login.php>Kthehu prapa</a>"; exit; } // Lets attempt to move the file from its temporary directory to its new home if (move_uploaded_file($image['tmp_name'], $TARGET_PATH)) { // NOTE: This is where a lot of people make mistakes. // We are *not* putting the image into the database; we are putting a reference to the file's location on the server $sql = "INSERT INTO admins_form (title, name, surname, email, phone, city, comments, price, date_added, filename) values ('$title', '$name', '$surname', '$email', '$phone', '$city', '$comments', '$price', now(), '" . $image['name'] . "')"; $result = mysql_query($sql) or die ("Futja e te dhenave ne databaze DESHTOI. <a href=login.php>Provo perseri</a> "); echo "Shtimi i te dhenave u krye me SUKSES. Klikoni per te shkuar tek <a href=../index.php>faqja kryesore</a>"; exit; } else { // A common cause of file moving failures is because of bad permissions on the directory attempting to be written to // Make sure you chmod the directory to be writeable echo "Shtimi i te dhenave NUK u krye me Sukses. Ju lutem provoni <a href=login.php>perseri</a>"; exit; } } here is the form: <p>Te gjithe fushat qe jane te shoqeruara me nje yll (*) duhen plotesuar!</p> <form action="adminform_submit.php" method="post" enctype="multipart/form-data" name="frm" id="frm"> <table width="980" border="0" cellpadding="6" cellspacing="0" class="inputfield"> <tr> <th scope="row">Title:</th> <th colspan="2" scope="row"><span id="sprytextfield1"> <input type="text" name="title" id="title" tabindex="5" /> <span class="textfieldMaxCharsMsg">Keni kaluar numrin maksimal te shkronjave.</span><span class="textfieldRequiredMsg">Kjo zbrazetire duhet plotesuar.</span></span> *</th> </tr> <tr> <th scope="row"><label>Emri: </label></th> <th colspan="2" scope="row"><span id="sprytextfield2"> <input type="text" name="name" id="name" tabindex="10" /> <span class="textfieldRequiredMsg">Kjo zbrazetire duhet plotesuar.</span><span class="textfieldMaxCharsMsg">Keni kaluar numrin maksimal shkronjave.</span></span> *</th> </tr> <tr> <th scope="row"><label>Mbiemri: </label></th> <th colspan="2" scope="row"><input type="text" name="surname" id="surname" tabindex="20" /></th> </tr> <tr> <th scope="row"><label>Email: </label></th> <th colspan="2" scope="row"><span id="sprytextfield3"> <input type="text" name="email" id="email" tabindex="30" /> <span class="textfieldRequiredMsg">Kjo zbrazetire duhet plotesuar.</span><span class="textfieldInvalidFormatMsg">Shkruani emailin ne formen e duhur.</span><span class="textfieldMaxCharsMsg">Keni kaluar numrin maksimal te shkronjave.</span></span> *</th> </tr> <tr> <th scope="row"><label>Nr. Kontaktues: </label></th> <th colspan="2" scope="row"><input type="text" name="phone" id="phone" tabindex="40" /></th> </tr> <tr> <th scope="row"><label>Qyteti: </label></th> <th colspan="2" scope="row"><span id="sprytextfield4"> <input type="text" name="city" id="city" tabindex="50" /> <span class="textfieldRequiredMsg">Kjo zbrazetire duhet plotesuar.</span><span class="textfieldMaxCharsMsg">Keni kaluar numrin maksimal te shkronjave.</span></span> *</th> </tr> <tr> <th scope="row"><label>Informatat e<br /> hollesishme: </label></th> <th colspan="2" scope="row"><span id="sprytextarea1"> <textarea name="comments" id="comments" cols="45" rows="5" tabindex="60"></textarea> <span class="textareaRequiredMsg">Kjo zbrazetire duhet plotesuar.</span></span> *</th> </tr> <tr> <th scope="row"><label>Fotografite: </label></th> <th colspan="2" scope="row"><input type="file" name="image" id="image" tabindex="70" /> *</th> </tr> <tr> <th scope="row"><label>Cmimi: </label></th> <th colspan="2" scope="row"><span id="sprytextfield5"> <input type="text" name="price" id="price" tabindex="80" size="10"/> <span class="textfieldRequiredMsg">Kjo zbrazetire duhet plotesuar.</span><span class="textfieldMaxCharsMsg">Keni kaluar numrin maksimal te shifrave te lejuara.</span><span class="textfieldInvalidFormatMsg">Ju lutem shkruani cmimin ne formen e duhur.</span></span> *</th> </tr> <tr> <th scope="row"><input type="submit" name="submit" id="submit" value="Shtoj te dhenat" tabindex="90" /></th> <th scope="row"> </th> <th scope="row" align="right"><a href="logout.php">Dalja</a></th> </tr> </table> </form> Quote Link to comment Share on other sites More sharing options...
haris244808 Posted September 28, 2011 Author Share Posted September 28, 2011 Cmon man is there anyone can solve my problem... Quote Link to comment Share on other sites More sharing options...
xyph Posted September 28, 2011 Share Posted September 28, 2011 You're not really asking for help are you? This isn't your code you're trying to modify. Understand the code above, and you should understand how to make it work for multiple files. Quote Link to comment Share on other sites More sharing options...
haris244808 Posted September 28, 2011 Author Share Posted September 28, 2011 You're not really asking for help are you? This isn't your code you're trying to modify. Understand the code above, and you should understand how to make it work for multiple files. r u kidding? of course its not all mine. I seen a lot of codes than i modified one. I understand all the code and i know i should use a loop to make multiple file upload, but whatever i tried it wont work so i asked here for help. and isnt ur job to help us? Quote Link to comment Share on other sites More sharing options...
TOA Posted September 28, 2011 Share Posted September 28, 2011 and isnt ur job to help us? Actually, no. All staff, gurus, etc are volunteers if I'm not mistaken. Quote Link to comment Share on other sites More sharing options...
haris244808 Posted September 28, 2011 Author Share Posted September 28, 2011 and isnt ur job to help us? Actually, no. They are all volunteers if I'm not mistaken. exactly when u r volunteer u help otherwise u dont need to reply, because its php forum not for chatting dont misunderstand. this is the truth Quote Link to comment Share on other sites More sharing options...
TOA Posted September 28, 2011 Share Posted September 28, 2011 exactly when u r volunteer u help otherwise u dont need to reply, because its php forum not for chatting dont misunderstand. this is the truth LMAO Ok. Lets see how much help you get telling people they have to help you. BTW..you could use an array to store the multiple files Quote Link to comment Share on other sites More sharing options...
haris244808 Posted September 28, 2011 Author Share Posted September 28, 2011 exactly when u r volunteer u help otherwise u dont need to reply, because its php forum not for chatting dont misunderstand. this is the truth LMAO Ok. Lets see how much help you get telling people they have to help you. BTW..you could use an array to store the multiple files haha thanks. btw i just got one Quote Link to comment Share on other sites More sharing options...
TOA Posted September 28, 2011 Share Posted September 28, 2011 haha thanks. btw i just got one Got what? Quote Link to comment Share on other sites More sharing options...
haris244808 Posted September 28, 2011 Author Share Posted September 28, 2011 haha thanks. btw i just got one Got what? a help man Quote Link to comment Share on other sites More sharing options...
TOA Posted September 28, 2011 Share Posted September 28, 2011 a help man Let's just say I'm feeling generous Just telling you. Not everyone would help with that approach. Notice he hasn't posted again? Anyway... make your form element an array, and add a loop around the processing for each file in the input array Quote Link to comment Share on other sites More sharing options...
haris244808 Posted September 28, 2011 Author Share Posted September 28, 2011 a help man Let's just say I'm feeling generous Just telling you. Not everyone would help with that approach. ok man i understood. i started the topic as a generous too Quote Link to comment Share on other sites More sharing options...
TOA Posted September 28, 2011 Share Posted September 28, 2011 Gotcha. Here's from the manual on how to do it Quote Link to comment Share on other sites More sharing options...
haris244808 Posted September 28, 2011 Author Share Posted September 28, 2011 Gotcha. Here's from the manual on how to do it thnx man. in the database i have only one field for holding the path of the file do i need to create any other for multiple. (and after the upload of the pictures i will need to echo them out on the table) Quote Link to comment Share on other sites More sharing options...
TOA Posted September 28, 2011 Share Posted September 28, 2011 No extra fields would be needed if I understand you correctly. Each image would be stored as a new record. Depends on your table structure though. If you can post that it might help To echo, just loop through the directory where they're stored and display them Quote Link to comment Share on other sites More sharing options...
haris244808 Posted September 28, 2011 Author Share Posted September 28, 2011 No extra fields would be needed if I understand you correctly. Each image would be stored as a new record. Depends on your table structure though. If you can post that it might help To echo, just loop through the directory where they're stored and display them aha ok. however when i put the code to this loop: foreach ($_FILES["image"]["error"] as $key => $error) {} and make the name of the box with [] still doesnt work.. The table i use its normal one that contains 4 columns for the picture . Quote Link to comment Share on other sites More sharing options...
TOA Posted September 28, 2011 Share Posted September 28, 2011 No extra fields would be needed if I understand you correctly. Each image would be stored as a new record. Depends on your table structure though. If you can post that it might help To echo, just loop through the directory where they're stored and display them aha ok. however when i put the code to this loop: foreach ($_FILES["image"]["error"] as $key => $error) {} and make the name of the box with [] still doesnt work.. The table i use its normal one that contains 4 columns for the picture . Can you show all your relevant code? Quote Link to comment Share on other sites More sharing options...
haris244808 Posted September 28, 2011 Author Share Posted September 28, 2011 No extra fields would be needed if I understand you correctly. Each image would be stored as a new record. Depends on your table structure though. If you can post that it might help To echo, just loop through the directory where they're stored and display them aha ok. however when i put the code to this loop: foreach ($_FILES["image"]["error"] as $key => $error) {} and make the name of the box with [] still doesnt work.. The table i use its normal one that contains 4 columns for the picture . Can you show all your relevant code? here is the php: [m]<?php //check if the button is pressed if(isset($_POST['submit'])){ //Connect to database require_once('../Connections/connect_to_mysql.php'); foreach ($_FILES["image"]["error"] as $key => $error) { // Check to see if the type of file uploaded is a valid image type function is_valid_type($file) { // This is an array that holds all the valid image MIME types $valid_types = array("image/jpg", "image/jpeg", "image/bmp", "image/gif", "image/png"); if (in_array($file['type'], $valid_types)) return 1; return 0; } // This variable is the path to the image folder where all the images are going to be stored // Note that there is a trailing forward slash $TARGET_PATH = "adminform_images/"; $image = $_FILES['image']; $image['name'] = mysql_real_escape_string($image['name']); // Build our target path full string. This is where the file will be moved do // i.e. images/picture.jpg $TARGET_PATH .= $image['name']; // Make sure all the fields from the form have inputs if ( $image['name'] == "" ) { echo "Te gjithe fushat duhet plotesuar. <a href=login.php>Provo perseri</a>"; exit; } // Check to make sure that our file is actually an image // You check the file type instead of the extension because the extension can easily be faked if (!is_valid_type($image)) { echo "Fotografite duhet te jene te formateve jpg, jpeg, bmp, gif, ose png. <a href=login.php>Kthehu prapa</a>"; exit; } // Here we check to see if a file with that name already exists // You could get past filename problems by appending a timestamp to the filename and then continuing if (file_exists($TARGET_PATH)) { echo "Ju lutem nderroni emrin e fotos dhe provoni perseri. <a href=login.php>Kthehu prapa</a>"; exit; } // Lets attempt to move the file from its temporary directory to its new home if (move_uploaded_file($image['tmp_name'], $TARGET_PATH)) { // Get our POSTed variables $title = $_POST['title']; $name = $_POST['name']; $surname = $_POST['surname']; $email = $_POST['email']; $phone = $_POST['phone']; $city = $_POST['city']; $comments = $_POST['comments']; $price = $_POST['price']; // Sanitize our inputs $title = mysql_real_escape_string($title); $name = mysql_real_escape_string($name); $surname = mysql_real_escape_string($surname); $email = mysql_real_escape_string($email); $phone = mysql_real_escape_string($phone); $city = mysql_real_escape_string($city); $comments = mysql_real_escape_string($comments); $price = mysql_real_escape_string($price); if ( $title == "" || $name == "" || $email == "" || $city == "" || $comments == "" || $price == "") { echo "Te gjithe fushat duhet plotesuar. <a href=login.php>Provo perseri</a>"; exit; } // NOTE: This is where a lot of people make mistakes. // We are *not* putting the image into the database; we are putting a reference to the file's location on the server $sql = "INSERT INTO admins_form (title, name, surname, email, phone, city, comments, price, date_added, filename) values ('$title', '$name', '$surname', '$email', '$phone', '$city', '$comments', '$price', now(), '" . $image['name'] . "')"; $result = mysql_query($sql) or die ("Futja e te dhenave ne databaze DESHTOI. <a href=login.php>Provo perseri</a> "); echo "Shtimi i te dhenave u krye me SUKSES. Klikoni per te shkuar tek <a href=../index.php>faqja kryesore</a>"; exit; } else { // A common cause of file moving failures is because of bad permissions on the directory attempting to be written to // Make sure you chmod the directory to be writeable echo "Shtimi i te dhenave NUK u krye me Sukses. Ju lutem provoni <a href=login.php>perseri</a>"; exit; } } } ?>[/m] and here is the input file for the image: <input type="file" name="image[]" id="image" tabindex="70" /> 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.