如果你对该文章中的内容有疑问/不解,可以点击此处链接提问
要注明问题和此文章链接地址 点击此处跳转
html <meta name="csrf-token" content="{{csrf_token()}}"> <div class="layui-card"> <div class="layui-card-header">封面图</div> <div class="layui-card-body layui-row layui-col-space8"> <div class="layui-col-md12"> <input id="main_pic" type="text" name="thumb" lay-verify="required" placeholder="" autocomplete="off" class="layui-input" value=""> </div> <span type="button" class="layui-btn" id="up_img">上传图片</span> </div> </div> upload.render({ elem: '#up_img' ,url: "upload_one" ,data:{'_token':$('meta[name="csrf-token"]').attr('content')} ,done: function(res){ console.log(res) if(res.result==1){ layer.msg("上传成功", {icon: 1}); $("#main_pic").val(res.msg); }else{ layer.msg('上传失败', {icon: 1}); } } }); 路由 Route::post('/upload_one','\App\Http\Controllers\Admin\CommonController@upload_one'); //方法 public function upload_one(Request $request){ if ($request->isMethod('POST')) { //判断是否是POST上传 $fileCharater = $request->file('file'); if ($fileCharater->isValid()) { //括号里面的是必须加的哦 //获取文件的扩展名 $ext = $fileCharater->getClientOriginalExtension(); //获取文件的绝对路径 $path = $fileCharater->getRealPath(); //定义文件名 $filename = time().'_'.mt_rand(1,999).'.'.$ext; //存储文件。disk里面的public。总的来说,就是调用disk模块里的public配置 $aa = \Storage::disk('public')->put($filename, file_get_contents($path)); return jssuccess(\Storage::url($filename)) ; } } } 配置 config/filesystems.php 'default' => env('FILESYSTEM_DRIVER', 'public'), 'public' => [ 'driver' => 'local', 'root' => storage_path('app/public'), 'url' => env('APP_URL').'/storage/app/public', 'visibility' => 'public', ], 命令行执行php artisan storage:link