ThinkPHP V5.0 图片上传base64

/**
 * 单图上传
 * @return bool|mixed|string
 */
protected function uploading_qrcode($images = '',$name ='')
{
    if(empty($images)) $this->result($images, 0, '图片错误');
    if (preg_match('/^(data:\s*image\/(\w+);base64,)/',$images,$result)){
        $type = $result[2];//图片后缀
        $new_file =Env::get('root_path') . 'public' . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'images';//文件路径
        $url = DIRECTORY_SEPARATOR . date("Ymd", time()) . DIRECTORY_SEPARATOR;//文件路径
        $new_file=$new_file.$url;//文件路径
        if (!file_exists($new_file)) {
            //检查是否有该文件夹,如果没有就创建,并给予最高权限
            mkdir($new_file, 0777,true);
        }

        $filename = $name. ".{$type}"; //文件名
        $new_file = $new_file . $filename;//全部路径
        $url = $url.$filename;//存储路径
        //写入操作
        if(file_put_contents($new_file, base64_decode(str_replace($result[1], '', $images)))) {
            $url = 'uploads' . DIRECTORY_SEPARATOR . 'images' . $url;;
            return $url;
        }
    }
}

评论

暂无评论