/**
* 文件上传
* @param string $filename
* @param string $path
* @param array $type
* @return array
*/
function upload_file($filename='',$path='',$type=[]){
$max_filesize = 8388608;//8M
if(empty($type))
$type = ['xls','xlsx','jpeg','png','jpg','gif'];
if(empty($path))
$path = ROOT_PATH . 'public' . DS . 'uploads'.DS;
if(empty($filename))
return ['code'=>'-1','msg'=>'文件名不能为空'];
$file = request()->file($filename);
if($file){
if($file->getSize() <= $max_filesize){
$info = $file->move($path);
if(!in_array($info->getExtension(),$type)){
unlink($path.$info->getSaveName());
return ['code'=>'-5','msg' => '文件类型有误'];
}elseif($info){
//上传成功
$msg = array(
'ext' => $info->getExtension(),
'path' => $path.$info->getSaveName(),
'filename'=> $info->getFilename()
);
return ['code'=>1,'msg'=>$msg];
}else{
//上传失败,获取错误信息
return ['code'=>0,'msg'=>$info->getError()];
}
}elseif($file->getSize() > $max_filesize){
return ['code'=>'-3','msg' => '上传的文件不能大于8M'];
}else{
return ['code'=>'-4','msg'=>'上传文件异常'];
}
}else{
return ['code'=>'-2','msg'=>'上传文件失败'];
}
}
网友评论