×

php保存base64图片引发的安全问题

chen chen 发表于2021-07-27 20:21:37 浏览542 评论0

抢沙发发表评论

function base64_image($base64,$path){
    //匹配出图片的格式
    if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64, $result)){
        $type = $result[2];
        $new_file = $path."/".date('Ymd',time())."/";
        if(!file_exists($new_file)){           
            mkdir($new_file, 0700);
        }
        $new_file = $new_file.time().".{$type}";
        if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64)){
            return '/'.$new_file;
        }else{
            return false;
        }
    }else{
        return false;
    }
}

在网上找的base64存图片的方法,拿来主义直接就用了。没有对图片格式做校验,悲剧了。

image/php;base64,
这样的也能通过上面的正则验证。
在存储的时候需要对$type格式进行验证。

群贤毕至

访客