写上传 首先 在 routes/web.php 写路由
Route::any('upload', 'BootController@upload');
写完路由后,进行上传配置 在 config 目录下的filesystems.php
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'), //app/public 这个可以修改
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
],
],
完成功后开始写控制器
public function upload(Request $request){
//判断是否是POST上传
if ($request->isMethod('POST')) {
$fileCharater = $request->file('file');
if ($fileCharater->isValid()) {//括号里面的是必须加的,不然会报错
//获取文件的扩展名
$ext = $fileCharater->getClientOriginalExtension();
//获取文件的绝对路径
$path = $fileCharater->getRealPath();
//定义文件名
$filename = date('Y-m-d-h-i-s').'.'.$ext;
//存储文件。disk里面的public。总的来说,就是调用disk模块里的public配置
$res=Storage::disk('public')->put($filename, file_get_contents($path));
var_dump($res);
}
}
}
前台代码
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap 实例 - 基本表单</title>
<link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css">
<script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://apps.bdimg.com/libs/bootstrap/3.3.0/js/bootstrap.min.js"></script>
</head>
<body>
<form role="form" action="upload" method="post" enctype="multipart/form-data">
{{csrf_field()}}
<div class="form-group">
<input type="file" name="file">
</div>
<button type="submit" class="btn btn-default">提交</button>
</form>
</body>
</html>
完成后你的图片上传到了 storage 目录下的app下的public里。这个不是固定的你可以通过上面的配置修改