dongyixun0634 2017-03-07 08:20
浏览 56
已采纳

错误(为foreach()提供的参数无效)使用php上传多个图像时

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");    
}    
?>
  • 写回答

2条回答 默认 最新

  • dongmangji0950 2017-03-07 08:25
    关注

    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'];
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记