php图片上传base64数据编码。

Posted on 2018-11-26 16:07  jiaoqing。  阅读(322)  评论(0)    收藏  举报

/**
* base64图片上传

*/
function IdImg($base64_img = ''){
$up_dir = 'upload/';//存放在当前目录的upload文件夹下
if(!file_exists($up_dir)){
mkdir($up_dir,0777);
}
if(preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_img, $result)){
$type = $result[2];
if(in_array($type,array('pjpeg','jpeg','jpg','gif','bmp','png','jgp'))){
$new_file = $up_dir.date('YmdHis').'.'.$type;
if(file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_img)))){
$img_path = str_replace('./', '', $new_file);
return $img_path;
}else{
return '图片上传失败';
}
}else{
return '图片上传类型错误';
}
}else{
return '上传文件错误';
}
}

博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3