Laravel--单文件和多文件上传

                              Laravel--单文件和多文件上传

laravel中单文件上传:

实现代码:

        $file = Request::file('photo');//获取文件值  在此我们要在头部掉用use Illuminate\Http\Request;路由get传值
        $name = input::get('name');//其他属性值 
        $allowed_extensions = ["png", "jpg", "gif"];
        if ($file->getClientOriginalExtension() && !in_array($file->getClientOriginalExtension(), $allowed_extensions)) {
            return ['error' => 'You may only upload png, jpg or gif.'];
        }
        $destinationPath = 'storage/uploads/'; //public 文件夹下面建 storage/uploads 文件夹
        $extension = $file->getClientOriginalExtension();
        $fileName = str_random(10).'.'.$extension;
        $file->move($destinationPath, $fileName);
        $filePath = asset($destinationPath.$fileName);
        $info=DB::insert('insert into photo(pname,photo) VALUES (?,?)',[$name,$filePath]);//入库
          if($info){
           return Redirect('/show');
          }else{
              echo "no";
          }

在laravel中我没有找到多文件上传

在此我自己封装了一个多文件上传希望能帮到各位学友们;

朋友们路由我没写清楚 你们自己要注意点路由问题啦!

案例:

        $file = $res->file("img_name");
        foreach ($file as $key => $value) {
             if(!empty($value)){//此处防止没有多文件上传的情况
               $allowed_extensions = ["png", "jpg", "gif"];
               if ($value->getClientOriginalExtension() && !in_array($value->getClientOriginalExtension(), $allowed_extensions)) {
                  return ['error' => 'You may only upload png, jpg or gif.'];die;
               }
               $destinationPath = 'storage/uploads/'; //public 文件夹下面建 storage/uploads 文件夹
               $extension = $value->getClientOriginalExtension();
               $fileName = str_random(10).'.'.$extension;//重命名
               $value->move($destinationPath, $fileName);
               $filePath = asset($destinationPath.$fileName);
               $post['landlord_img']="storage/uploads/".$fileName;
               $list=array('img_name'=>$fileName,'house_id'=>$id);
               DB::table('img')->insert($list);                 
           }
         }

 

 

 

 

 

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值