Laravel文件上传

Laravel文件系统是基于Frank de Jonge的Flysystem扩展包
提供了简单的接口,可以操作本地端空间、AmazonS3、Rackspace Cloud Storage
可以非常简单的切换不同的保存方式,但仍使用相同的API操作
支持 local ftp s3 rackspace
默认是local 就是本地
s3是亚马逊的配置,在国内用的很少,国内可以弄个阿里云OSS 七牛存储
本人laravel版本是5.2

然后我们新建一个控制器方法,然后添加对应的路由

Route::any('/upload',"StudentController@upload");

public function upload(Request $request){
   if($request->isMethod('POST')){
      //var_dump($_FILES);
      //exit;
      $file = $request->file('files');
      //dd($file);
      //判断文件是否上传成功
      if($file->isValid())
      {
         //原文件名
         $originalName = $file->getClientOriginalName();
         //文件扩展名
         $ext = $file->getClientOriginalExtension();
         //文件MImeType
         $type = $file->getClientMimeType();
         //文件临时绝对路径
         $realPath = $file->getRealPath();
         //为了避免重名
         $filename = date('YmdHis').'-'.uniqid().'.'.$ext;
         echo $filename;
         $bool = Storage::disk('uploads')->put($filename,file_get_contents($realPath));
         //var_dump($bool);
      }
      echo 'ok';
   }
   return view('student.upload');
}

模板文件是根据auth下面login.blade.php修改的如下:

@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">Login</div>
                <div class="panel-body">
                    <form class="form-horizontal" role="form" method="POST" action="{{ url('/login') }}">
                        {{ csrf_field() }}

                        <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
                            <label for="email" class="col-md-4 control-label">E-Mail Address</label>

                            <div class="col-md-6">
                                <input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}">

                                @if ($errors->has('email'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('email') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
                            <label for="password" class="col-md-4 control-label">Password</label>

                            <div class="col-md-6">
                                <input id="password" type="password" class="form-control" name="password">

                                @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">
                                <div class="checkbox">
                                    <label>
                                        <input type="checkbox" name="remember"> Remember Me
                                    </label>
                                </div>
                            </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> Login
                                </button>

                                <a class="btn btn-link" href="{{ url('/password/reset') }}">Forgot Your Password?</a>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

OK,完成了,测试下

发表评论

电子邮件地址不会被公开。 必填项已用*标注