因?yàn)樽龅氖莂pp的接口,里面的make_arr()方法是給前臺的json格式數(shù)據(jù)
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('','參數(shù)錯誤','4');die;
}
}