//php上传base64类型图片
public function multi_imgupload()
{
$jsdata = html_entity_decode($_POST["jsdata"]);
$jsdatass = json_decode($jsdata, true);
$data=array();
$msg="";
$code="0";
if($jsdatass) {
foreach ($jsdatass as $key => &$value) {
$imgname = $value['imgname'];
$imgbase64 = $value['imgbase64'];
//图片上传
if (preg_match('/^(data:\s*image\/(\w+);base64,)/',$imgbase64,$result)){
$type = $result[2];//图片后缀
$new_file =ROOT_PATH . 'attachs' . DS ;
$url='/ueditor/imagebase64/'. date("Ymd", time()) . "/";
$new_file=$new_file.$url;
if (!file_exists($new_file)) {
//检查是否有该文件夹,如果没有就创建,并给予最高权限
mkdir($new_file, 0777,true);
}
$filename = time() . '_' . uniqid() . ".{$type}"; //文件名
$new_file = $new_file . $filename;
$url=$url.$filename;
//写入操作
if(file_put_contents($new_file, base64_decode(str_replace($result[1], '', $imgbase64)))) {
$data_img = array();
$data_img['imgurl'] = $url;
$data_img['imgname'] =$imgname;
$data_img['create_time'] = time();
$pid = Db::table("wxb_imgurlknowledge")->insertGetId($data_img);
$arr = array("url" => $url, "id" => $pid);
$url_top = 'http://';
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
$url_top = 'https://';
}
$url=$url_top.$_SERVER['HTTP_HOST'].'/attachs'.$url;
$code="1";
$msg="上传成功";
$data[]=array('name'=>$imgname,'url'=>$url);
}
}
}
}
else
{
$code="0";
$msg="上传失败,图片参数为空,请上传图片";
}
return $this->result($data, $code, $msg, 'json');
}
// 接收参数 jsdata [{"imgname":1945,"imgbase64":1},{"imgname":1946,"imgbase64":1}]
//返回参数 { "code": 1, "msg": "上传成功", "time": 1575442476, "data": [ { "name": "22002.jpg", "url": /ueditor/imagebase64/20191204/1575442476_5de7582c45396.png" }, { "name": "11002.jpg", "url": "/ueditor/imagebase64/20191204/1575442476_5de7582c49401.png" } ] }
php上传base64类型图片
最新推荐文章于 2023-07-27 15:36:41 发布