传入格式为Base64格式
//传入Base64图片
public static function upFile($file){
if(!empty($file)){
if(!preg_match('/^(data:\s*image\/(\w+);base64,)/', $file, $result)){
throw new ParameterException([
'msg' => '编码错误'
]);
}
$type = $result[2];
if (strstr($file,",")){
$image = explode(',',$file);
$file = $image[1];
}
$file_name = date('YmdHis',time()).mt_rand(1000,9999) . '.' . $type ;
$file_path = './upload/'. date("Y") .'/'. date("m") . '/' .date('d');
$imageSrc= $file_path."/". $file_name;
if(!is_dir($file_path)){
mkdir($file_path,0777,true);
}
file_put_contents($imageSrc, base64_decode($file));
$new_path = '域名'.substr($file_path,1) .'/'. $file_name;
return $new_path;
}
}