因为做的是app的接口,里面的make_arr()方法是给前台的json格式数据
protected function make_arr($data, $message = '成功',$other = '0')
{
Tool::convertArrayNullValue($data);
if($data && $other == '0') {
$rdata['status'] = "1";
$rdata['message'] = $message;
$rdata['data'] = $data;
}else if($other <> '0'){
$rdata['status'] = $other;
$rdata['message'] = $message;
}else{
$rdata['status'] = "0";
$rdata['message'] = '失败';
}
return json_encode($rdata);
}
base64图片上传
$img是前台给的图片base64
public function upfile(){
$img = I('img');//图片base64 用 ”|“符号隔开
$img = explode('|', $img);
if($img){
foreach($img as $v){
$base64_img = trim($v);
$up_dir = 'Public/Console/images/home/'.date('Y',time()).'/'.date('m',time());
if(!is_dir($up_dir)){
mkdir($up_dir,0777,true);
}
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'))){
$new_name = time().uniqid();
$new_file = $up_dir.'/'.$new_name.'.'.$type;
$base64_1 = str_replace($result[1],'', $base64_img);
if(file_put_contents($new_file, base64_decode($base64_1))){
$pic = '/'.$new_file;
$nnpic = $pic.','.$nnpic;
}else{
echo $this->make_arr('','上传失败','0');die;
}
}else{
//文件类型错误
echo $this->make_arr('','文件类型错误','2');die;
}
}else{
//文件错误
echo $this->make_arr('','base64错误','3');die;
}
}
if($nnpic){
echo $this->make_arr($nnpic,'成功');die; //返回用逗号隔开的上传后的图片路径
}
}else{
echo $this->make_arr('','参数错误','4');die;
}
}