将图像保存到数据库中

k2arahey  于 2021-06-20  发布在  Mysql
关注(0)|答案(0)|浏览(238)

我正在尝试将图像保存到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;
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题