0

I am working on my project I want to upload images with single form but I have two input fields. I want when I choose the both images on both input fields when I submit it I want my both files are stored in the folder I show you my code but with this code, I am getting only one image. I want both images which I am uploaded. my form looks like this

enter image description here

Here is my code: public function upload_creative_workshop_image($post, $files){ //print_r($files); die; $allowedExts= array("jpeg", "jpg", "png", "gif"); $temp = explode(".", $files["image1"]["name"]); $extension = end($temp); if ((($files["image1"]["type"] == "image/jpeg") //check image is mp3 || ($files["image1"]["type"] == "image/gif") //check image is mp4 || ($files["image1"]["type"] == "image/jpg") //check image is jpg || ($files["image1"]["type"] == "image/png")) //check image is png && ($files["image1"]["size"]) //check if image size is below 6MB && in_array(trim($extension), $allowedExts)) //check the extensions also { if($files["image1"]["error"] > 0){ echo $files["image1"]["error"]; } else{ $filename = $files["image1"]["name"]; $upload= move_uploaded_file($files["image1"]["tmp_name"], $this->img_path.$filename); //print_r($upload); die; if($upload){ //$this->store_image_indb($filename, $post); } } }else{ echo "<h1>please upload image only</h1>"; } } 
5
  • You have to call your function for each image you want to upload. In this case, your inputfields should have different names like image1 and image2. BUT what I would do is to make your input field multiple and then call your function once with the images in $files array. For that you have to add a loop into your function. Commented Jul 26, 2017 at 11:53
  • yeah I have both fields are the different name like this: Commented Jul 26, 2017 at 11:58
  • <td><input type="file" name="image1" value="<?php echo $image1;?>"></td> Commented Jul 26, 2017 at 11:59
  • <input type="file" name="image2" value="<?php echo $image2;?>"> Commented Jul 26, 2017 at 11:59
  • what do you see, when you call var_dump($_FILES)? Commented Jul 27, 2017 at 10:14

2 Answers 2

1

If you have two different names like 'image1' and 'image2' then please check below solution:

You have to run loop of $_FILES array like below and have to modify your upload function slightly as below.

<?php if (isset($_FILES)) { if (COUNT($_FILES) >= 1) { foreach ($_FILES as $i_key => $image_file) { $testObject = new test(); $testObject->upload_creative_workshop_image($image_file); } } } class test { function upload_creative_workshop_image($image){ //print_r($files); die; $allowedExts= array("jpeg", "jpg", "png", "gif"); $temp = explode(".", $image["name"]); $extension = end($temp); if ((($image["type"] == "image/jpeg") //check image is mp3 || ($image["type"] == "image/gif") //check image is mp4 || ($image["type"] == "image/jpg") //check image is jpg || ($image["type"] == "image/png")) //check image is png && ($image["size"]) //check if image size is below 6MB && in_array(trim($extension), $allowedExts)) //check the extensions also { if($image["error"] > 0){ echo $image["error"]; } else{ $filename = $image["name"]; $upload= move_uploaded_file($image["tmp_name"], 'uploads/'.$filename); //print_r($upload); die; if($upload){ //$this->store_image_indb($filename, $post); } } }else{ echo "<h1>please upload image only</h1>"; } } } ?> 
Sign up to request clarification or add additional context in comments.

5 Comments

This is not right code with this code m facing many errors and your code have many errors some variables are not matched with loop and functions
This is perfectly running code I tried it my self. If u know oops then you can run it without error.
yeah i am using in this project but when i use this code i am facing error can u tell me this php code where should i place it means which file m not talking about function m talking about top code which u tell me
You should add that $_FILES loop when u Submit form and inside this loop only u should call function.
can u send me ur skype id and linked in id i can share my files with you and properly discuss and m also girl u can if u want please tell me
0

You can use multitype form data like

<form method=post action='' enctype='multipart/form-data'> <input type=file name='images[]'> 

3 Comments

i also use this
<form name="form" method="post" action="" enctype="multipart/form-data" onsubmit="return validate()">
@kulu can you provide me with the JS code side? on return validate()

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.