php - 我无法显示数据库中的图像
问题描述
我正在尝试创建一个在线购物系统,该系统将显示和添加带有图像的产品。我已经观看并按照视频教程进行操作,但我似乎无法显示数据库中的图像。
索引文件
$sql = "SELECT * FROM tblproducts ORDER BY placeOrder DESC";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt , $sql)) {
echo "SQL statement failed!";
} else {
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
while ($row = mysqli_fetch_assoc($result)){
echo"
<div><img src='".$row['image']."' uploads/></div>
<h3>".$row["imageTitle"]."</h3>
<p>".$row["prod_description"]."</p>";
}
}
上传文件
if (isset($_POST['submit'])) {
$newFileName = $_POST['filename'];
if (empty($newFileName)) {
$newFileName = "tblproducts";
} else {
$newFileName = strtolower(str_replace(" ","-", $newFileName));
}
$imageTitle = $_POST['filetitle'];
$imageDesc = $_POST['filedesc'];
$file = $_FILES['file'];
$fileName = $file["name"];
$fileType = $file["type"];
$fileTempName = $file["tmp_name"];
$fileError = $file["error"];
$fileSize = $file["size"];
$fileExt = explode(".", $fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array("jpg", "jpeg", "png");
if (in_array($fileActualExt, $allowed)) {
if ($fileError === 0) {
if ($fileSize < 2000000) {
$imageFullName = $newFileName . "." . uniqid("", true) . "." . $fileActualExt;
$fileDestination = "uploads/" . $imageFullName;
include_once "conn.php";
if (empty($imageTitle) || empty($imageDesc)) {
header("Location: ../index.one.php?upload=empty");
exit();
}else{
$sql = "SELECT * FROM tblproducts;";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
echo "SQL statement failed! ";
} else {
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$rowCount = mysqli_num_rows($result);
$setImageOrder = $rowCount + 1;
$sql = "INSERT INTO tblproducts (imageTitle, prod_description, image, placeOrder) VALUES (?, ?, ?, ?);";
if (!mysqli_stmt_prepare($stmt, $sql)) {
echo "SQL statement failed! ";
} else {
mysqli_stmt_bind_param($stmt, "ssss",$imageTitle, $imageDesc, $imageFullName, $setImageOrder);
mysqli_stmt_execute($stmt);
move_uploaded_file($fileTempName, $fileDestination);
header('location:index.one.php?productadded');
照片被保存在文件目的地和数据库中的数据中。除图像外,所有细节都在显示。
解决方案
您需要选择图像数据并使用正确的 MIME 类型等发送出去。
这是我对文件附件执行的强制下载操作
<?php
// gives me executeQuery which does pdo prepared query and returns an array
// where [0] is true or false, and [1] is either error message (if 0 false)
// or [1..N] are the rows returned from a select
include('PDO.inc.php');
$q="SELECT file_data, file_mime, file_size, file_name FROM fileStorage where pk=?";
$a=array(33);
$res=executeQuery($q,$a);
if(!$res[0]){
print("<h2><font color=red>Error</font> ".$res[1]."</h2>");
exit;
}
header("Content-length: ".$res[1]['file_size']);
header("Content-type: ".$res[1]['file_type']);
header("Content-Disposition: attachment; filename=".$res[1]['file_name']);
echo $res[1]['file_data'];
?>
如果您想做诸如将图像嵌入网页而不强制“另存为...”之类的操作,您可以根据存储的 mime 类型确定图像类型,然后使用适当的 ImageMagick 东西,如imagecreatefrompng (),发送适当的内容头,然后用 imagepng ()发送图像数据本身
推荐阅读
- php - 显示几个 woo-commerce 产品类别的特定页面模板
- matlab - 如何为轮廓设置 uicontextmenu (MATLAB2014b)
- spring-boot - JSP文件未加载到浏览器中,但文件名在spring Boot Application中的浏览器中显示为字符串
- vba - 如何通过 Selenium 和 VBA 根据 html 从第二行和第二列中提取文本 8
- android - 在 android studio 3.1.4 中不能使用“?attr/actionBarSize”
- arrays - 如何使用 IText 按大小按 PDF 拆分并存储到字节数组中
- java - Java初始化类
- database - 数据库语言中的队列城市位置独立
- angular - 来自 CDN 的 Angular 2
- apache-spark - 减少 Apache Spark 启动时间