我正在尝试将图像保存在mysql数据库中。它将二进制代码保存到表中,但是它损坏了,我无法显示它。另外,数据库字段的大小大于原始文件的大小。我的代码或所采用的方法有什么问题吗?
我知道这不是将图像存储在数据库中的好习惯,但是我无法决定它。
<div class="box-body">
<h3>Bilder</h3>
<form id="image_form">
<input type="file" name="frame" id="file_name" />
<input type="submit" value="Save">
</form>
</div>
$('#image_form').on("submit",(function (e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: './index.php?action=585',
data: new FormData(this),
contentType: false,
cache: false,
processData: false,
beforeSend: function (a) {
//a.overrideMimeType('image/jpeg; charset=UTF-8');
},
success: function (data) {
console.log(data);
},
error: function () {
location.reload();
}
});
}));
后端
//Get the content of the image and then add slashes to it
$tmpName = $_FILES['frame']['tmp_name'];
$fp = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
$data = addslashes($data);
var_dump($data);
$imagetmp = $data;
AccountDAO::updateResImages($imagetmp, $imagetmp, $imagetmp, $_SESSION['customer']->id);
数据库
public static function updateResImages($img1, $img2, $img3, $customerid){
$db = new MySQLDatabase();
$sql = "UPDATE customer SET online_res_bild1 = ?, online_res_bild2 = ?, online_res_bild3 = ? WHERE customer_id = ? ";
$rs = $db->executeQuery($sql, 'sssi', $img1, $img2, $img3, $customerid);
$db->close();
return 1;
}