您的位置:首页 > Web前端

php 接前端传来的图片,进行base64分解,从新入库

2017-07-17 17:40 555 查看
public function add(){
$base642_image_content=$_POST['mainimage'];
  if(empty($base642_image_content)){
$response = array(
'code' => 1,
'message' => '商品主图不能为空',
);
echo json_encode($response);exit;
}
 if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base642_image_content, $result)){
$type = $result[2];
$date=date('Ym');
$rand=rand(1,9999999999);
$url= dirname(dirname(dirname(dirname(dirname(dirname(__FILE__))))));
//print_r($url);die;
if (!is_dir($url.'/images/'.$date))
mkdir($url.'/images/'.$date); // 如果不存在则创
$new_file = "$url/images/".$date."/goods_img/$date$rand.{$type}";
if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base642_image_content)))){

}
}
$data['goods_img']="images/$date/goods_img/$date$rand.{$type}";
if ($this->model->table('goods')->data($data)->add()) {$response = array('code' => 200,'message' => '提交成功',);echo json_encode($response);}else {$response = array('code' => 202,'message' => '填写信息失败',);echo json_encode($response);exit;}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: