kulu
kulu

Reputation: 43

regarding multiple files upload in single form

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>";
        }
}

Upvotes: 0

Views: 512

Answers (2)

Himani
Himani

Reputation: 61

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>";
    }
}
}
?>

Upvotes: 1

Andrew
Andrew

Reputation: 13

You can use multitype form data like

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

Upvotes: 0

Related Questions