MP145 Posted October 19, 2008 Share Posted October 19, 2008 Hi, i have read some tutorials and manage to put a script together to upload an image and data to my database. The image is uploaded to a folder and the file name is inserted into the database. The script below works like a charm. However, I would like to know, how can i upload multiple images at once ? My form code <form enctype="multipart/form-data" action="add.php" method="POST"> Name: <input type="text" name="name"><br> E-mail: <input type="text" name = "email"><br> Phone: <input type="text" name = "phone"><br> Photo: <input type="file" name="photo"><br> <input type="submit" value="Add"> </form> I understand that i can add another line of Photo: <input type="file" name="photo1"><br> to the form page above, but how can i add it to the code below; My add.php code <?php $conn = mysql_connect('localhost','root','kannan') or trigger_error("SQL", E_USER_ERROR); $db = mysql_select_db('sample',$conn) or trigger_error("SQL", E_USER_ERROR); //This is the directory where images will be saved $target = "images/"; $target = $target . basename( $_FILES['photo']['name']); //This gets all the other information from the form $name=$_POST['name']; $email=$_POST['email']; $phone=$_POST['phone']; $pic=($_FILES['photo']['name']); $sql = "INSERT INTO employees (name, email, phone, photo) VALUES ('$name', '$email', '$phone', '$pic')"; $result = mysql_query($sql) or die ("Error in query: $query. ".mysql_error()); //Writes the photo to the server if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) { //Tells you if its all ok echo "The file ". basename( $_FILES['uploaded']['name']). " has been uploaded, and your information has been added to the directory"; } else { //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } ?> I am running Apache 2.2.8 , PHP 5.2.6 and MySQL 5.0.5 as localhost Thank you very much. Link to comment https://forums.phpfreaks.com/topic/129109-uploading-multiple-images-with-php-and-mysql/ Share on other sites More sharing options...
PFMaBiSmAd Posted October 19, 2008 Share Posted October 19, 2008 http://us.php.net/manual/en/features.file-upload.multiple.php Link to comment https://forums.phpfreaks.com/topic/129109-uploading-multiple-images-with-php-and-mysql/#findComment-669307 Share on other sites More sharing options...
MP145 Posted October 19, 2008 Author Share Posted October 19, 2008 Thanks PFMaBiSmAd I will try, but looks very confusing Link to comment https://forums.phpfreaks.com/topic/129109-uploading-multiple-images-with-php-and-mysql/#findComment-669316 Share on other sites More sharing options...
MP145 Posted October 19, 2008 Author Share Posted October 19, 2008 OK, that particular site was difficult to understand. I read a few others and got some samples and did this: <?php $conn = mysql_connect('localhost','xxx','yyyy') or trigger_error("SQL", E_USER_ERROR); $db = mysql_select_db('sample',$conn) or trigger_error("SQL", E_USER_ERROR); $imageName1 = $_FILES['image1']['name']; $tmpImageName1 = $_FILES['image1']['tmp_name']; $imageName2 = $_FILES['image2']['name']; $tmpImageName2 = $_FILES['image2']['tmp_name']; $imageName3 = $_FILES['image3']['name']; $tmpImageName3 = $_FILES['image3']['tmp_name']; $imageName4 = $_FILES['image4']['name']; $tmpImageName4 = $_FILES['image4']['tmp_name']; $imageName5 = $_FILES['image5']['name']; $tmpImageName5 = $_FILES['image5']['tmp_name']; $imagePath1 = 'images/' . $imageName1; $imagePath2 = 'images/' . $imageName2; $imagePath3 = 'images/' . $imageName3; $imagePath4 = 'images/' . $imageName4; $imagePath5 = 'images/' . $imageName5; if( file_exists($tmpImageName1) ){ move_uploaded_file($tmpImageName1, $imagePath1); } if( file_exists($tmpImageName2) ){ move_uploaded_file($tmpImageName2, $imagePath2); } if( file_exists($tmpImageName3) ){ move_uploaded_file($tmpImageName3, $imagePath3); } if( file_exists($tmpImageName4) ){ move_uploaded_file($tmpImageName4, $imagePath4); } if( file_exists($tmpImageName5) ){ move_uploaded_file($tmpImageName5, $imagePath5); } $sql = "INSERT INTO images (image1, image2, image3, image4, image5) VALUES ('$imageName1', '$imageName2', '$imageName3', '$imageName4', '$imageName5')"; mysql_query($sql) or die ("Error in query: $sql. ".mysql_error()); echo "<h4>Images uploaded</h4><br />"; ?> But it's very long, can i know if i can shortened the codes? bcoz i need to add other details like name and contact too. Thanks Link to comment https://forums.phpfreaks.com/topic/129109-uploading-multiple-images-with-php-and-mysql/#findComment-669372 Share on other sites More sharing options...
PFMaBiSmAd Posted October 19, 2008 Share Posted October 19, 2008 But it's very long, can i know if i can shortenedYes, use an array like the page at the link showed. You can then use a foreach() loop it iterate over all the values. The User Contributed Notes section at that link shows several examples using an array and a foreach() loop. This example is the one I would start with - http://us.php.net/manual/en/features.file-upload.multiple.php#55614 Link to comment https://forums.phpfreaks.com/topic/129109-uploading-multiple-images-with-php-and-mysql/#findComment-669381 Share on other sites More sharing options...
MP145 Posted October 19, 2008 Author Share Posted October 19, 2008 Thanks for the reply PFMaBiSmAd But how can i input the names of each image into the database ? Thank you Link to comment https://forums.phpfreaks.com/topic/129109-uploading-multiple-images-with-php-and-mysql/#findComment-669388 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.