laravel框架之批量添加文件

view视图中的代码

<body>
<form action="{{url('fuxi_yi')}}" method="post" enctype="multipart/form-data" >
    {{csrf_field()}}
    用户名:<input type="text" name="user[]"><br>
    密码:<input type="text" name="pwd[]"><br>
    头像:<input type="file" name="img[]"><br>
    <span class="zheli"></span><br>
    <input type="submit">
    <input type="button" name="tj" onclick="fun()" value="添加">
</form>
</body>
</html>
<script src="{{asset('assets')}}/js1/jquery.min.js"></script>
<script>
    function fun() {
        var tr = ("<input type='text' name='user[]'><br><input type='text' name='pwd[]'><br> <input type='file' name='img[]'><br>");
        $('.zheli').append(tr+'<br>');
    }
</script>

 

controller控制器中的代码

public function zhuce(){
      //  echo 123;die;
        $s = new Fuxi();
        $s->zhuce();
           $list = DB::table('fuxi')->select()->get();
            return view('Fuxi/Fuxi_xianshi',['list'=>$list]);
    }

 

model模型中的代码

 //这个方法完成批量添加信息   以及批量添加文件
    public function zhuce(){
        //获取前台传来的所有数据
        $list = request()->all();
        $img = request()->file('img');
        //用来计数看看传来几组数据
        $count = count($list['pwd']);
        //申请一个空数据  用来存放所有的图片
        $str = [];
        for($i=0;$i<$count;$i++){
           /* $_POST['user'][$i]*/
           //这里进行循环    有几组数据   就自动组成几个随机文件名称
            array_push($str,date('YmdHis').rand(100,9999).'.'.'png');
            $img[$i]->move('./image/',$str[$i]);
            //这里存放进一个数组  然后进行插入
            $data[] = [
                'user'=>$_POST['user'][$i],
                'pwd'=>$_POST['pwd'][$i],
                'img'=>$str[$i]
            ];
            //有几条数据 向数据库插入几次    以完成批量添加
            DB::table('fuxi')->insert($data);
        }
      // $img =  file_get_contents('./image/201811181126171418.png');


    }

 

路由

Route::any('fuxi_yi',"Fuxicontroller\Fuxicontroller@zhuce");

 显示数据 显示添加的所有内容view视图

<table>
    <tr>
        <th>用户名</th>
        <th>密码</th>
        <th height="50px" width="50px">头像</th>
        <th >操作</th>
    </tr>

    @foreach($list as $v)
        <tr>
            <td>{{$v->user}}</td>
            <td>{{$v->pwd}}</td>
            <td><img src="{{asset('image')}}/{{$v->img}}" height="50px" width="50px"></td>
            <td><a href="{{url('')}}">修改</a>丨<a href="{{url('')}}">删除</a></td>
        </tr>
    @endforeach


</table>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值