PHP base64多图片上传

    // 多图片上传,base64
    public function upload_multi() {
        $pic = $_POST['pic'];
        if (!$pic) {
            $this->json->setErr('10001',lang('tips_param_error'));
            $this->json->Send();
        }

        $err = 0;
        $out_data = [];
        if (is_array($pic)) {
            foreach ($pic as $k=>$v) { // 处理base64图片数据
                $img =  $this->base64_to_img($v);
                if (!$img) {
                    $err ++;
                }
                $out_data[] = $img;
            }
        } else {
            // 处理base64图片数据
            $img =  $this->base64_to_img($pic);
            if (!$img) {
                $err ++;
            }
            $out_data[] = $img;
        }

        if($err == 0){
            // 处理凭证绑定
            $this->json->setErr(0,lang('tips_deal_success'));
            $this->json->setAttr('data',$out_data);
            $this->json->Send();
        }else{
            $this->json->setErr(10099, lang('tips_deal_fail'));
            $this->json->Send();
        }
    }

    // base64=>img
    public function base64_to_img($base64_str,$us = 'img') {
        //  $base_img是获取到前端传递的src里面的值,也就是我们的数据流文件
        if (strstr($base64_str,",")){
            $base64_str_arr = explode(',',$base64_str);
            $base64_str = $base64_str_arr[1];
        }
        //  设置文件路径和文件前缀名称
        $folders = date('Ymd',time());
        $path = "site_upload/".$us.'/'.$folders.'/';
        if (!is_dir($path)){
            @mkdir('./'.$path, 0777,true);
        }
        $prefix='rf_';
        $output_file = $prefix.time().rand(100,999).'.jpg';
        $img_path = $path.$output_file;
        //  创建将数据流文件写入我们创建的文件内容中
        $ifp = fopen( $img_path, "wb" );
        fwrite( $ifp, base64_decode( $base64_str) );
        fclose( $ifp );
        // 输出文件
        $qiniu = new QiniuImg();

        $img =  config('user.sf_host'). $img_path;
        $ext = pathinfo($img, PATHINFO_EXTENSION);
        $name = time() . mt_rand() . '.' . $ext;
        $s = $qiniu->up($img, $name, config('user.qiniu.bucket'));

        @unlink('./' .$img_path);

        if($s){
            return config('user.cdn_host') . $name;
        } else {
            return false;
        }
}
原文地址:https://www.cnblogs.com/jiqing9006/p/9706051.html