0

When i m uploading multiple image in database then i m getting error like:- Invalid argument supplied for foreach()

My Code:-

<form name="" method="post" enctype="multipart/form-data" action="add_venue_image_do.php">
    <div class="col-lg-12" style=" margin-bottom:16px;">
        <label>Venu Image</label>
        <input multiple required type="file" name="multi_image" class="form-control">
        <input id="venue_hidden_id" value="5" name="venue_hidden_id" type="hidden"> //value 5 indicate business id
        <button type="submit" value="Submit" style="margin-top:15px;" class="btn btn-primary btn-sm pull-right submitMultiImg">Submit </button>
    </div>
</form>

add_venue_image_do.php:-

when PHP code excute foreach() then it's throwing error Invalid argument supplied for foreach()

<?php
if(isset($_COOKIE['login']))
{
    require("../../root/db_connection.php");
    if(isset($_REQUEST['venue_hidden_id'])){
        $venue_hidden_id=$_REQUEST['venue_hidden_id'];

        foreach($_FILES['image']['tmp_name'] as $key => $tmp_name ): //getting error on this line
            $file_name = $key.$_FILES['multi_image']['name'][$key];
            $file_size =$_FILES['multi_image']['size'][$key];
            $file_tmp =$_FILES['multi_image']['tmp_name'][$key];
            $file_type=$_FILES['multi_image']['type'][$key];    
            $img_ext= pathinfo($file_name,PATHINFO_EXTENSION);

            $q=$db->query("insert into venue_image(v_b_id,v_i_ext,v_img_created_date)
            values($venue_hidden_id,'$img_ext',now())") or die("error");

            $lastID= $db->lastInsertId();
            $imageNewName=$lastID.".".$img_ext;
            move_uploaded_file($file_tmp,"../../venue_image//".$imageNewName);
        endforeach;
        ?>

< script >
    alert('Record Updated Successfully');
    window.location.replace('edit_venue.php?v_id=5');
    < /script>
<?php
    }
    else    {
        ?> < script >
    window.location.replace('add_venue.php');
    < /script>
<?php
    }    
}    
else
{    
    header("location:../index.php");    
}    
?>
  • Please follow below link: [http://stackoverflow.com/questions/24895170/multiple-image-upload-php-form-with-one-input](http://stackoverflow.com/questions/24895170/multiple-image-upload-php-form-with-one-input) – Dipali Sakle Systematix Mar 07 '17 at 09:17

2 Answers2

1

Change your html code

<input type="file" name="files[]" multiple/>

Your php code should be

foreach($_FILES["files"]["tmp_name"] as $key=>$tmp_name)
            {
// image uploading code 
}

Reference link

Community
  • 1
  • 1
0

Your input name only allow for single parameters, if you want to support multiple files, you will need to change to

<input multiple required type="file[]" name="multi_image" class="form-control">

and loop with php

<?php
foreach ($_FILES['file'] as $key => $file) {
    $tmpName = $file['tmp_name'];

}
Đào Minh Hạt
  • 2,742
  • 16
  • 20