/**
* @param $file
* @param array $type_allow 允许上传的文件类型
* @param int $size_max 允许上传文件的大小 默认8M
* @return array
*/
function uploadFile($file,$type_allow=[],$size_max=8388608){
if(empty($type)){
$type_allow=[
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',//xlsx
'application/vnd.ms-excel',//xls
'image/jpeg',//jpg/jpeg
'image/png',//png
'image/gif'//gif
];
}
//查看文件是否上传到临时路径
if($file->isValid())
{
// $original_name = $file->getClientOriginalName();//原文件名
$ext = $file->getClientOriginalExtension();//扩展名
$realPath = $file->getRealPath();//临时文件的绝对路径
//判断文件类型
$type = $file->getClientMimeType();//文件类型
if(!in_array($type,$type_allow)) return ['code'=>'failure','error_msg'=>'文件类型不对'];
//判断文件大小
$size = $file->getClientSize();
if($size>$size_max) return ['code'=>'failure','error_msg'=>'文件太大'];
$filename = date('Y-m-d-H-i-s').'-'.uniqid().'.'.$ext;
config(['filesystems.disks.public.root' => app('path.public').'/uploads']);//重新设置上传路径,不用配置的
$bool = Storage::disk('public')->put($filename,file_get_contents($realPath));//移动文件到指定路径
$path = config('filesystems.disks.public.root');//获取上传后的全路径
if($bool)
{
return [
'code' =>'successful',//状态
'filename' =>$filename,//文件名
'path_name' =>'uploads/'.$filename,//相对路径文件名
'fullpath_name'=>$path.'/'.$filename,//全路径文件名
];
}
else {
return ['code'=>'failure','error_msg'=>'文件上传异常'];
}
}
else{
return ['code'=>'failure','error_msg'=>'文件上传失败'];
}
}
网友评论