laravel+layui后台文件上传到本地

上传图片

PHP控制器

//文件上传
    public function upload(Request $request)
    {
        //上传文件最大大小,单位M
        $maxSize = 1;
        //支持的上传图片类型
        $allowed_extensions = ["png", "jpg", "gif"];
        //返回信息json
        $data = ['code'=>1, 'msg'=>'上传失败', 'data'=>''];
        $file = $request->file('file');
        $filename = $request->all('filename')['filename'];
        //检查文件是否上传完成
        if ($file->isValid()){
            //检测图片类型
            $ext = $file->getClientOriginalExtension();
            if (!in_array(strtolower($ext),$allowed_extensions)){
                $data['msg'] = "请上传".implode(",",$allowed_extensions)."格式的图片";
                return response()->json($data);
            }
            //检测图片大小
            if ($file->getSize() > $maxSize*1024*1024){
                $data['msg'] = "图片大小限制".$maxSize."M";
                return response()->json($data);
            }
        }else{
            $data['msg'] = $file->getErrorMessage();
            return response()->json($data);
        }
        $year = date('Y');
        $date = date('m-d');
        $newFile = md5(uniqid()).".".$file->getClientOriginalExtension();
        $disk = Storage::disk('uploads');
        $res = $disk->put('/'.$filename.'/'.$year.'/'.$date.'/'.$newFile,file_get_contents($file->getRealPath()));
        if($res){
            $data = [
                'code'  => 0,
                'msg'   => '上传成功',
                'data'  => $newFile,
                'url'   => '/uploads/'.$filename.'/'.$year.'/'.$date.'/'.$newFile,
            ];
        }else{
            $data['data'] = $file->getErrorMessage();
        }
        return response()->json($data);
    }

js:

//普通图片上传
        $(".uploadPic").each(function (index,elem) {
            upload.render({
                elem: $(elem)
                ,url: '{{ route("api.upload") }}'
                ,multiple: false
                ,data:{"_token":"{{ csrf_token() }}","filename":'links'}
                ,done: function(res){
                    //如果上传失败
                    if(res.code == 0){
                        var _li = $(elem).parent('.layui-upload').find('.layui-upload-box .li_thumb_'+index+'');
                        _li.show();
                        layer.msg(res.msg,{icon:1});
                        _li.html('<div><img title="点击删除" src="'+res.url+'" /></div><p>上传成功</p>');
                        $(elem).parent('.layui-upload').find('.thumb_'+index+'').val(res.url);
                        
                    }else {
                        layer.msg(res.msg,{icon:2});
                    }
                }
            });
        });

html:

<div class="layui-form-item">
    <label for="" class="layui-form-label">缩略图</label>
    <div class="layui-input-block">
        <div class="layui-upload">
            <button type="button" class="layui-btn layui-btn-sm uploadPic"><i class="layui-icon"></i>图片上传</button>
            <div class="layui-upload-list" >
                <ul class="layui-upload-box layui-clear">
                    <li class="li_thumb_0" id="thumb_0" style="@if(isset($links->thumb)) display: block; @else display: none; @endif"><div><img title="点击删除" src="{{$links->thumb??old('thumb')}}" /></div><p>上传成功</p></li>
                </ul>
                <input type="hidden" name="thumb" class="layui-upload-input thumb_0" value="{{$links->thumb??old('thumb')}}">
            </div>
        </div>
    </div>
</div>


点赞0
点击评论0
收藏0
浏览 18
 

还没有评论,快来发表第一个评论吧

免责声明:凡在本网站出现的信息,均仅供参考。本网站将尽力确保所提供信息的准确性及可靠性,但不保证有关资料的准确性及可靠性,注册用户和一般页面游览者在使用前请进一步核实,并对任何自主决定的行为负责。本网站对有关资料所引致的错误、不确或遗漏,概不负任何法律责任(包括侵权责任、合同责任和其它责任)
*尊重作者,转载请注明出处!