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条)

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系