将文件插入 mysql 时出现问题


Issue inserting file into mysql

我有一个html表单(+引导),相关部分在这里:

<div class="form-group">
    <label for="inputPic">Item Image</label>
    <input type="file" name="inputPic">
    <p class="help-block">Upload a picture</p>
</div>

用户按提交并被带到另一个检查文件扩展名的 php 文件:

if (!empty($_FILES['inputPic']['tmp_name']))
{
    if ($_FILES['inputPic']['type'] == "image/jpeg" || $_FILES['inputPic']['type'] == "image/jpg" || $_FILES['inputPic']['type'] == "image/png")
    {
        if ($content = file_get_contents($_FILES['inputPic']['tmp_name']))
        {
            $image = addslashes($content);
        }
    }
}

最后尝试插入到 sql 中:

if (isset($image))
{
    if ($stmt = $mysqli->prepare("INSERT INTO posted_items (seller, post_date, expiration, image, description, name, category, startBid, buyNowPrice, minPrice, sold) VALUES (?, NOW(), ?, ?, ?, ?, ?, ?, ?, ?, 0)"))
    {
        $stmt->bind_param("issssiddd", $userid, $endDateAndTime, $image, $description, $itemName, $itemCategory, $startBid, $buyNow, $reservation);
        $stmt->execute();
        $stmt->close();
    }
}

sql 插入工作正常,因为我在我的数据库中看到它,但图像(大斑点类型)根本没有显示给我。图像字段中的数据库中有一堆随机数据,但是当我调用

echo '< img class="img-responsive centered" src="data:image/jpeg;base64,'.base64_encode($row['image']).'"></img>';

我什么也看不见。我哪里出错了?

谢谢!

您应该将图像上传到服务器上的某个目录,并将路径保存在数据库中。