我想使用php web服务将base64编码的图像保存到数据库中。 具体来说,这是表格的定义,它将保留它:
CREATE TABLE `message` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(45) DEFAULT NULL,
`content` varchar(250) DEFAULT NULL,
`latitude` double DEFAULT NULL,
`longitude` double DEFAULT NULL,
`date` datetime DEFAULT NULL,
`picture` blob,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=latin1;
正如您所看到的,我将列图片创建为blob。然后,服务器将获得如下对象:
{
"title": "hellrrerer",
"content": "norereerthe content",
"latitude": null,
"longitude": 12,
"date": "2009-04-30 10:09:00",
"picture": "/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/2wBDAQMDAwQDBAgEBAgQCwkLEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBD/wgARCACWASwDAREAAhEBAxEB/8QAHAABAAIDAQEBAAAAAAAAAAAAAAYHBAUIAgMB/8QAFAEBAAAAAAAAAAAAAAAAAAAAAP/aAAwDAQACEAMQAAAB5bAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABMCxj2UMejoUwCtyHl8GKRkrcmxY5ilLGcW2bsqwgheZjmGVAZB0GaMrwiYAAANsXQRcqkHRJFyLEHOhjTldkVOgCgD8APsXYUaDoQr8l5RgOoCKlIHwAAABlExLUOdj4nQhVhDgX4ViYBoCzDZmOVoYZ9y6yjQXgag1hXIOhiqyOGAAAAATw0BogSsigBJjbmIQ0EtPBFj8P0lJFQSgi5MSHAmpkmlI2AAAAAAAAAAAAAAAAAAAAAAAAAAAACwzybs2RmkdKmA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xEDTYikWNB6QcWuEHoehUHF0WmMHBGXRndwnkTogZ4ABhVdQxYf5OZnApl6dP+yAVDX63xQysL7oy6cgqYB/wwSoJSAuogpoA1vEh0kXRS01CtQKNFGM0dBJzrABJEuswoIT2sPiJiQjYqhdmy8OJ9fPBQpdGGbfA6ih2q3z3UyB87BCV9T3tenIh5fNn1xi02Gc48pVC4B5VGzSmIKah2N6URBgKRMH2yTVV5Vbf0Dri4MJxCjhWsqL3WLiUD7faOwBtWHGqjdFkMZCKk0WAFvhntOgYe17Br90f/9k="
}
我会尝试使用以下方法将其保存到数据库中:
$insertStmt = $mysqli -> prepare('INSERT INTO message (title, content, latitude, longitude,date, picture) VALUES (?,?,?,?,?,?)');
$insertStmt -> bind_param('ssddsb', $message['title'], $message['content'], $message['latitude'], $message['longitude'],
$message['date'],$message['picture']);
现在我知道这不起作用,因为图片仍然是一个字符串;我的问题是我应该怎么做才能将它转换为将数据库保存为blob的格式? 感谢
答案 0 :(得分:-2)
尝试使用file_get_contents保存图片。
$sql = "INSERT INTO Table(columns, ....)
VALUES(....,'" . file_get_contents($yourImage) . "')";
您可以进一步阅读THIS主题
此外,您可以使用base64_encode(如果您愿意)
对图像进行编码