1.app上传图片使用http上传的,用的form表单,设置form属性为enctype="multipart/form-data"
后台获取用$_FILES就可以获取了,和网页上传图片类似!
2.或者
/**
* 封装base64位图片上传
*/
function base64_upload($base64)
{
$base64_image = str_replace(' ', '+', $base64);
//post的数据里面,加号会被替换为空格,需要重新替换回来,如果不是post的数据,则注释掉这一行
if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image, $result)) {
//匹配成功
if ($result[2] == 'jpeg') {
$image_name = uniqid() . '.jpg';
//纯粹是看jpeg不爽才替换的
} else {
$image_name = uniqid() . '.' . $result[2];
}
$image_file = "./public/upload/jl_qd/{$image_name}";
//服务器文件存储路径
if (file_put_contents($image_file, base64_decode(str_replace($result[1], '', $base64_image)))) {
return '/public/upload/jl_qd/' . $image_name;
} else {
return false;
}
} else {
return false;
}
}
public function image(){
//宝贝图片-------------------
$photo_list = $request_data['image'];
$photo_list = json_decode($photo_list, true);
$count_photo = count($photo_list);
if($count_photo == 0){
return json_encode(array('code' => 1 , 'msg' => '请选择图片' , 'data' =>''));
}
if($count_photo < 4){
return json_encode(array('code' => 1 , 'msg' => '请上传全部的图片' , 'data' =>''));
}
$photo = "";
if($count_photo > 1){
foreach ($photo_list as $k => $v) {
$photo .= $this->base64_upload($v['base64']).",";
}
$photo = substr($photo,0,strlen($photo)-1);
}else{
foreach ($photo_list as $k => $v) {
$photo .= $this->base64_upload($v['base64']);
}
}
$where['image'] = $photo;//宝贝图片
//宝贝图片----------------
}