Reputation: 27
I'm trying to upload 3 images fields, store them in my database and move the uploaded file in a folder.
For now, the "image1", 'image2", "image3", "image4" fields are inserting into MySQL database, but only "image1" is uploading and moving into my folder.
Here is my code:
if(isset($_POST['submit'])){
$pro_image1 = $_FILES['image1']['name'];
$pro_image2 = $_FILES['image2']['name'];
$pro_image3 = $_FILES['image3']['name'];
$pro_image4 = $_FILES['image4']['name'];
$tmp_name = $_FILES['image1']['tmp_name'];
$tmp2_name = $_FILES['image2']['tmp_name'];
$tmp3_name = $_FILES['image3']['tmp_name'];
$tmp4_name = $_FILES['image4']['tmp_name'];
$pro_query = "INSERT INTO product(image1,image2,image3,image4) VALUES('$pro_image1','$pro_image2','$pro_image3','$pro_image4')";
if(mysqli_query($con,$pro_query)){
$msg = "<p class='pull-right' style='color:green;'> Product Added successfully</p>";
$path = "images/$pro_image1";
if(move_uploaded_file($tmp_name, $path)) {
copy($path, "../$path");
}
if(move_uploaded_file($tmp2_name, $path)) {
copy($path, "../$path");
}
if(move_uploaded_file($tmp3_name, $path)) {
copy($path, "../$path");
}
if(move_uploaded_file($tmp4_name, $path)) {
copy($path, "../$path");
} elseif(!mysqli_query($con,$pro_query)) {
$insert_error = "<p class='pull-right' style='color:red;>Product didn't added</p>";
}
}
}
<form action="" method="post" enctype="multipart/form-data" class="form-font">
<div class="col-md-6">
<div class="form-group">
<label>Image1</label>
<input type="file" name="image1" class="form-control" required>
</div>
<div class="form-group">
<label>Image2</label>
<input type="file" name="image2" class="form-control" required>
</div>
<div class="form-group">
<label>Image3</label>
<input type="file" name="image3" class="form-control" required>
</div>
<div class="form-group">
<label>Image4</label>
<input type="file" name="image4" class="form-control" required>
</div>
<center>
<input type='submit' name='submit' class='btn btn-success' value='Add Product'>
</center>
</div>
</form>
How to move the other images ("image2", "image3", "image4") in my folder?
What is the mistake I made in my code? Could you please explain me where I am doing wrong ?
Upvotes: 2
Views: 1811
Reputation: 66
Here is what you need to do:
if(move_uploaded_file($tmp_name, $path)){
copy($path, "images/$pro_image2");
}
if(move_uploaded_file($tmp2_name, $path)){
copy($path, "images/$pro_image3");
}if(move_uploaded_file($tmp3_name, $path)){
copy($path, "images/$pro_image4");
}if(move_uploaded_file($tmp4_name, $path)){
//copy($path, "images/$path");
}
Upvotes: 2
Reputation: 216
please overrite your $path variable then it will upload property..
before if condition change $path varibale like below or change the variable to $path1,$path2,$path3..
$path = "images/$pro_image1";
$path = "images/$pro_image2";
$path = "images/$pro_image3";
-
if(isset($_POST['submit'])){
$pro_image1 = $_FILES['image1']['name'];
$pro_image2 = $_FILES['image2']['name'];
$pro_image3 = $_FILES['image3']['name'];
$pro_image4 = $_FILES['image4']['name'];
$tmp_name = $_FILES['image1']['tmp_name'];
$tmp2_name = $_FILES['image2']['tmp_name'];
$tmp3_name = $_FILES['image3']['tmp_name'];
$tmp4_name = $_FILES['image4']['tmp_name'];
$pro_query = "INSERT INTO product(image1,image2,image3,image4) VALUES('$pro_image1','$pro_image2','$pro_image3','$pro_image4')";
if(mysqli_query($con,$pro_query)){
$msg = "<p class='pull-right' style='color:green;'> Product Added successfully</p>";
$path = "images/$pro_image1";
if(move_uploaded_file($tmp_name, $path)){
copy($path, "../$path");
}
$path = "images/$pro_image2";
if(move_uploaded_file($tmp2_name, $path)){
copy($path, "../$path");
}
$path = "images/$pro_image3";
if(move_uploaded_file($tmp3_name, $path)){
copy($path, "../$path");
}
$path = "images/$pro_image4";
if(move_uploaded_file($tmp4_name, $path)){
copy($path, "../$path");
}
elseif(!mysqli_query($con,$pro_query)){
$insert_error = "<p class='pull-right' style='color:red;>Product didn't added</p>";
}
}
}
<form action="" method="post" enctype="multipart/form-data" class="form-font">
<div class="col-md-6">
<div class="form-group">
<label>Image1</label>
<input type="file" name="image1" class="form-control" required>
</div>
<div class="form-group">
<label>Image2</label>
<input type="file" name="image2" class="form-control" required>
</div>
<div class="form-group">
<label>Image3</label>
<input type="file" name="image3" class="form-control" required>
</div>
<div class="form-group">
<label>Image4</label>
<input type="file" name="image4" class="form-control" required>
</div>
<center><input type='submit' name='submit' class='btn btn-success' value='Add Product'></center>
</form>
Upvotes: 1
Reputation: 2327
You just need to named them like "image[0], image[1], image[2]" Changes in your code
<form action="" method="post" enctype="multipart/form-data" class="form-font">
<div class="col-md-6">
<div class="form-group">
<label>Image1</label>
<input type="file" name="image[0]" class="form-control" required>
</div>
<div class="form-group">
<label>Image2</label>
<input type="file" name="image[1]" class="form-control" required>
</div>
<div class="form-group">
<label>Image3</label>
<input type="file" name="image[2]" class="form-control" required>
</div>
<div class="form-group">
<label>Image4</label>
<input type="file" name="image[3]" class="form-control" required>
</div>
<center><input type='submit' name='submit' class='btn btn-success' value='Add Product'></center>
</form>
Upvotes: 0
Reputation: 147
You have var $path defined only for image1 -- you need to change this var within each "if" block for functions move_uploaded_file and copy. At the moment you use just path and filename for image1 for all images.
Upvotes: 0