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格式进行验证。