版权声明:本文为博主原创文章,未经博主允许不得转载。 https://cloud.tencent.com/developer/article/1333735
//自定义
'uploads' => [
'driver' => 'local',
//'root' => storage_path('app/uploads'),
'root' => public_path('uploads/'.date('Ymd')),
],
<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">
<label for="file" class="col-md-4 control-label">Hello world</label>
<div class="col-md-6">
<input id="file" type="file" class="form-control" name="source">
</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>
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
//上传文件 功能实现方法
public function upload(Request $request){
if ($request->isMethod('POST')){
$file = $request->file('source');
//判断文件是否上传成功
if ($file->isValid()){
//原文件名
$originalName = $file->getClientOriginalName();
//扩展名
$ext = $file->getClientOriginalExtension();
//MimeType
$type = $file->getClientMimeType();
//临时绝对路径
$realPath = $file->getRealPath();
$filename = uniqid().'.'.$ext;
$bool = Storage::disk('uploads')->put($filename,file_get_contents($realPath));
//判断是否上传成功
if($bool){
echo 'success';
}else{
echo 'fail';
}
}
}
return view('upload');
}