fastadmin(tp5)将base64保存成文件

将base64保存成文件,存于本地,可将此方法添加到common.php文件里,后面调用时,调用

$ret = saveBase64($base64_str)

 

function saveBase64($base64_img)
    {
        //目录的upload文件夹下
        $up_dir = ROOT_PATH . "public/uploads/".date('Ymd', time()) . "/";  //创建目录
        if(!file_exists($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'))){
                $name = \fast\Random::alnum(32);
                $new_file = $up_dir . $name . '.' . $type;
                if(file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_img)))){
                    return "/uploads/" . date('Ymd', time()) . "/" . $name . '.' . $type;
                }
            }
        }
        return false;
    }

 

posted @ 2022-06-01 17:42  贱贱丶  阅读(383)  评论(0编辑  收藏  举报