laravel单文件、多文件上传的实现方法
程序员文章站
2022-06-27 20:53:22
先设置上传文件的路由: Route::post('upload/images'['as'=>'uploadImages','uses'=>'UploadController@uploadImages']); Route::post('upload/multiUpload'['as'=>'multiU ......
先设置上传文件的路由:
route::post('upload/images'['as'=>'uploadimages','uses'=>'uploadcontroller@uploadimages']);
route::post('upload/multiupload'['as'=>'multiupload','uses'=>'uploadcontroller@multiupload']);
再设置uploads磁盘地址,之后储存文件会使用到。config / filesystem : disks
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'uploads'=>[
'driver'=>'local',
'root'=>public_path('uploads/'),
]
],
最后uploadcontroller定义上传函数(使用storage的disk方法来访问uploads磁盘,就是前面filesystem文件里设置的)
putfile方法:管理文件到指定的存储位置,例为自动生成文件名,也可以手动设置 ('20190705', $file,'test.png')
//上传单张图
public function uploadimages(request $request)
{
if ($request->ismethod('post')) {
$file = $request->file('file');
if($file->isvalid()){
$path = storage::disk('uploads')->putfile(date('ymd') , $file);
if($path) {
return ['code' => 0 , 'msg' => '上传成功' , 'data' => $path];
}
else {
return ['code' => 400 , 'msg' => '上传失败'];
}
}
} else {
return ['code' => 400, 'msg' => '非法请求'];
}
}
//上传多张图
public function multiupload(request $request)
{
if($request->method('post')){
$files = $request->allfiles();
if(is_array($files)){
foreach($files as $file){
$path = storage::disk('uploads')->putfile(date('ymd') , $file);
}
if( $path ) {
return ['code' => 0 , 'msg' => '上传成功' , 'data' => $path];
}
else {
return ['code' => 400 , 'msg' => '上传失败'];
}
}
}else{
return ['code' => 400, 'msg' => '非法请求'];
}
}
模板的上传操作 自行看layui文档吧,一毛一样的操作!!!
更多学习内容请访问:
腾讯t3-t4标准精品php架构师教程目录大全,只要你看完保证薪资上升一个台阶(持续更新)
下一篇: HCIE总结第四天