0

I have a PHP form that is supposed to allow the user to upload 5 images at once. All images should save into my website's folder images/ when the user hits submit. Currently, only the image in the first image input gets saved into the images/ folder. The image names are all saved into my MySQL table correctly, though.

Here is the code for my HTML form page:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<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>
 Photo: <input type="file" name="photo1"><br>
 Photo: <input type="file" name="photo2"><br>
 Photo: <input type="file" name="photo3"><br>
 Photo: <input type="file" name="photo4"><br> 
 <input type="submit" value="Add"> 
 </form>
</body>
</html>

And here is the add.php page code:

<?php 

 //This is the directory where images will be saved 
 $target = "images/"; 
 $target = $target . basename( $_FILES['photo']['name']); 
  $target1 = "images/"; 
 $target1 = $target1 . basename( $_FILES['photo1']['name']);
   $target2 = "images/"; 
  $target2 = $target2 . basename( $_FILES['photo2']['name']);
    $target3 = "images/"; 
   $target3 = $target3 . basename( $_FILES['photo3']['name']);
     $target4 = "images/"; 
    $target4 = $target4 . basename( $_FILES['photo4']['name']); 
 //This gets all the other information from the form 
 $name=$_POST['name']; 
 $email=$_POST['email']; 
 $phone=$_POST['phone']; 
 $pic=($_FILES['photo']['name']); 
  $pic1=($_FILES['photo1']['name']); 
   $pic2=($_FILES['photo2']['name']); 
    $pic3=($_FILES['photo3']['name']); 
     $pic4=($_FILES['photo4']['name']); 

 // Connects to your Database 
 mysql_connect("dnsacom", "ksbm", "Kszer") or die(mysql_error()) ; 
 mysql_select_db("keabm") or die(mysql_error()) ; 

 //Writes the information to the database 
 mysql_query("INSERT INTO `employees` VALUES ('$name', '$email', '$phone', '$pic', '$pic1', '$pic2', '$pic3', '$pic4')") ; 

 //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['uploadedfile']['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 image."; 
 }
  if(move_uploaded_file($_FILES['photo1']['tmp_name'], $target)) 
 { 

 //Tells you if its all ok 
 echo "The file ". basename( $_FILES['uploadedfile']['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 image."; 
 } 
  if(move_uploaded_file($_FILES['photo2']['tmp_name'], $target)) 
 { 

 //Tells you if its all ok 
 echo "The file ". basename( $_FILES['uploadedfile']['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 image."; 
 } 
  if(move_uploaded_file($_FILES['photo3']['tmp_name'], $target)) 
 { 

 //Tells you if its all ok 
 echo "The file ". basename( $_FILES['uploadedfile']['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 image."; 
 } 
  if(move_uploaded_file($_FILES['photo4']['tmp_name'], $target)) 
 { 

 //Tells you if its all ok 
 echo "The file ". basename( $_FILES['uploadedfile']['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 image."; 
 } 
 ?> 

Thank you for any help. All help is appreciated.

2
  • My first guess is that the images are too big and/or you're running out of memory on the server. Default upload size is 2 MB per image. How big is each image you're trying to upload? Commented Feb 18, 2014 at 23:14
  • The images I am trying to upload are about 15KB to 50KB each. Commented Feb 18, 2014 at 23:16

2 Answers 2

1

They all have the same $target variable. Change your $target variables to $target1, $target2, $target3 and $target4 and they'll upload.

if(move_uploaded_file($_FILES['photo1']['tmp_name'], $target1)) <--- CHANGE THESE

Edit: I tested your code and changed these variables and was able to upload all images.

Sign up to request clarification or add additional context in comments.

Comments

1

maybe check map permissions, i recommend checking out chmod, i can't find a problem in your code.

https://www.php.net/chmod or maybe use brackets since you upload multiple files? like type="file[]"

OW if(move_uploaded_file($_FILES['photo1']['tmp_name'], $target)) problem solved you use $target everywhere which is why only 1 picture gets uploaded

4 Comments

Sorry, I'm not familiar with how to use brackets. I will however check out chmod, as you suggested.
just a small question, are you doing this code locally, so server is running on your pc(xampp/wampp) and pictures are uploading locally, or are you using a real server, if it is the second answer then chmod could help you, if it is the first it would be unlikely.
I am using a real server. I don't think changing the file type of the images is the solution to my problem because as I already stated, the first image is uploading fine into the folder. It is the other images that I am having the issue with.
cool for the real server, but more ontopic, i think you should use an array, or upload them in a for loop, idk if it fixes your problem but its a very strange problem to me so worth a try i geuss. also does it even echo photo 1/2/3/4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.