在config/filesystems.php中新建本地端空间
'uploads' => [
'driver' => 'local',
'root' => storage_path('app/uploads'),
],
在控制器中添加一个方法
public function upload(Request $request)
{
if ($request->isMethod('POST')){
//var_dump($_FILES);
$file=$request->file('file');
//文件是否上传成功
if ($file->isValid()){
//原文件名
$originalName = $file->getClientOriginalName();
//扩展名
$ext = $file->getClientOriginalExtension();
//type
$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);
}
exit;
}
在view中的表单
<form class="form-horizontal" method="POST" action="" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="">
<label for="file" class="col-md-4 control-label">请选择文件</label>
<div class="col-md-6">
<input id="file" type="file" class="form-control" name="file" >
</div>
</div>
<div class="form-group">
<div class="col-md-8 col-md-offset-4">
<button type="submit" class="btn btn-primary">
确定上传
</button>
</div>
</div>
</form>
添加一条路由
Route::any('upload', 'StudentController@upload');
网友评论