laravel文件上传

f ($request->hasFile('attach')&&$request->file('attach')->isValid()) {
$file_attach = $request->file('attach');
//获取原文件名//
$originalName = $file_attach->getClientOriginalName();
//扩展名//
$ext = $file_attach->getClientOriginalExtension();
$type=$file_attach->getClientMimeType();// image/jpeg
$realPath = $file_attach->getRealPath(); //临时文件的绝对路径
$allow_type =['application/pdf'];
if (!in_array($type,$allow_type)){
return ['code'=>0,'msg'=>'文件类型不允许'];
}
$filename = date('Y-m-d-H-i-s') . '-' . uniqid() . '.' . $ext;
$bool = Storage::disk('public')->put($filename, file_get_contents($realPath));
if ($bool){
$class_id = DB::table('class')->where('major_id',$major_id)->select('id')->get()->toArray();
$ids = array_column($class_id,'id');
$class_student = DB::table('class_student')->whereIn('class_id',$ids)->where('user_id',$user_id)->first();
DB::table('class_student')->where(['class_id'=>$class_student->class_id,'user_id'=>$class_student->user_id])->update(['file_path'=>Storage::disk('public')->url($filename)]);
return ['code'=>1,'msg'=>'上传成功'];
}else{
return ['code'=>0,'msg'=>'上传失败'];
}
}

posted @ 2020-11-30 11:31  赵向上  阅读(162)  评论(0)    收藏  举报