PHP已知图片base64流上传到服务器
<?php $img=$_POST['basefile']; $data=saveBase64Image($img); var_dump($data); /** * 保存64位编码图片 */ function saveBase64Image($base64_image_content){ if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){ //图片后缀 $type = $result[2]; if($type=='jpeg'){ $type='jpg'; } //保存位置--图片名 $image_name=date('His').str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT).".".$type; $image_url = 'upload/'.date('Ymd').'/'.$image_name; if(!is_dir(dirname('./'.$image_url))){ mkdir(dirname('./'.$image_url)); chmod(dirname('./'.$image_url), 0777); // umask($oldumask); } //解码 $decode=base64_decode(str_replace($result[1], '', $base64_image_content)); if (file_put_contents('./'.$image_url, $decode)){ $data['code']='0'; $data['imageName']=$image_name; $data['image_url']=$image_url; $data['type']=$type; $data['msg']='保存成功!'; }else{ $data['code']='1'; $data['imgageName']=''; $data['image_url']=''; $data['type']=''; $data['msg']='图片保存失败!'; } }else{ $data['code']='1'; $data['imgageName']=''; $data['image_url']=''; $data['type']=''; $data['msg']='base64图片格式有误!'; } return $data; }