我有一个 html 表单(+ bootstrap),相关部分在这里:
<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 插入工作正常,但图像(largeblob 类型)根本没有显示出来。图像字段中的数据库中有一堆随机数据,但是当我调用时
echo '< img class="img-responsive centered" src="data:image/jpeg;base64,'.base64_encode($row['image']).'"></img>';
我什么也看不到。我哪里出错了?
谢谢!
最佳答案
您应该将图像上传到服务器上的某个目录并将路径保存在数据库中。
关于php - 将文件插入mysql时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27498285/