laravel基础之文件上传

来源:互联网 发布:淘宝卖家让我售后 编辑:程序博客网 时间:2024/06/05 07:29

使用laravel进行文件上传十分简单。以下贴出部分代码



文件上传方法代码

//文件上传方法    public function upload(Request $request) {        if($request->isMethod('POST')){//            var_dump($_FILES);            $file = $request->file('source');            //判断文件是否上传成功            if($file->isValid()){                //获取原文件名                $originalName = $file->getClientOriginalName();                //扩展名                $ext = $file->getClientOriginalExtension();                //文件类型                $type = $file->getClientMimeType();                //临时绝对路径                $realPath = $file->getRealPath();                $filename = date('Y-m-d-H-i-S').'-'.uniqid().'-'.$ext;                $bool = Storage::disk('uploads')->put($filename, file_get_contents($realPath));                var_dump($bool);            }            //dd($file);            /**            UploadedFile {#164 ▼            -test: false            -originalName: "填充文件.png"            -mimeType: "image/png"            -size: 10340            -error: 0            path: "D:\xampp\tmp"            filename: "phpF0E0.tmp"            basename: "phpF0E0.tmp"            pathname: "D:\xampp\tmp\phpF0E0.tmp"            extension: "tmp"            realPath: "D:\xampp\tmp\phpF0E0.tmp"            aTime: 2016-11-22 04:39:27            mTime: 2016-11-22 04:39:27            cTime: 2016-11-22 04:39:27            inode: 0            size: 10340            perms: 0100666            owner: 0            group: 0            type: "file"            writable: true            readable: true            executable: false            file: true            dir: false            link: false            linkTarget: "D:\xampp\tmp\phpF0E0.tmp"            }             */            exit;        }        return view('student.upload');    }


//文件上传路由设置Route::any('upload', 'StudentController@upload');

//        文件上传配置        'uploads' => [            'driver' => 'local',            'root' => storage_path('app/uploads'),        ],

文件上传view页面

@extends('layouts.app')@section('content')    <div class="container">        <div class="row">            <div class="col-md-8 col-md-offset-2">                <div class="panel panel-default">                    <div class="panel-heading">文件上传</div>                    <div class="panel-body">                        <form class="form-horizontal" role="form" method="POST" action="" enctype="multipart/form-data">                            {{ csrf_field() }}                            <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">                                <label for="file" class="col-md-4 control-label">请选择文件</label>                                <div class="col-md-6">                                    <input id="file" type="file" class="form-control" name="source">                                    @if ($errors->has('password'))                                        <span class="help-block">                                        <strong>{{ $errors->first('password') }}</strong>                                    </span>                                    @endif                                </div>                            </div>                            <div class="form-group">                                <div class="col-md-6 col-md-offset-4">                                    <button type="submit" class="btn btn-primary">                                        <i class="fa fa-btn fa-sign-in"></i> 确认上传                                    </button>                                </div>                            </div>                        </form>                    </div>                </div>            </div>        </div>    </div>@endsection



0 0
原创粉丝点击