我将数据库中的图像数据作为中间影片,并在用户通过相应的ID选择图片时尝试将其作为图像进行检索。在任何人跳下我的喉咙之前,是的我知道这是一个更平滑的过程来存储在文件服务器上并以那种方式检索它然而这是一个类,这就是教授想要的。
<HTML>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>BLOB Data Type Tutorial</title>
</head>
<body>
<form action="images.php" method="POST" enctype="multipart/form-data">
Select Picture: <input type="pictureID" name="pictureID"> <input type="submit" value="submit" name="submit">
</form>
<?php
$link = mysqli_connect('127.0.0.1:3306','user','');
$link;
mysqli_select_db($link, "media");
$sql = "SELECT * FROM images";
$result = mysqli_query($link,$sql);
echo "<table>";
echo "<tr><th>Picture</th><th>Size KB</th><th>Title</th></tr>";
if($result === FALSE){
echo "failed";
die(mysql_error());
}
while($row = mysqli_fetch_array($result)) {
$Picture = $row['Picture'];
$Size = $row['Size KB'];
$Title = $row['Title'];
echo "<tr><td style ='width: 50px;'>".$Picture."</td><td style='width: 60px;'>".$Size."</td><td>".$Title."</td></tr>";
}
echo "</table>";
if(isset($_POST['pictureID']))
{
$imgId = $_POST['pictureID'];
echo $imgId;
if (!empty($imgId)) {
$sqliCommand = "SELECT Picture FROM images WHERE Picture = $imgId";
$result = mysqli_query($link, $sqliCommand);
$row = mysqli_fetch_assoc($result);
header("Content-type: image/jpeg");
echo $row['Image'];
}
}
else
{
echo "^ Please select a picture! ^";
}
?>
<br>
<h4>
<a href="./index.html">Main Menu</a> <br>
</h4>
</body>
</HTML>
感谢任何提示/帮助!
答案 0 :(得分:0)
这是因为页面顶部还有html代码。执行header("Content-type: image/jpeg");
时,chrome会将整页内容解释为包含html的图像数据。
要解决此问题,请在id传递脚本顶部的代码时输出图像,并在输出图像后立即退出脚本,否则下面的代码也会输出。
应该这样做:
<?php
//this should be at top of php file, above html code
$link = mysqli_connect('127.0.0.1:3306','user','');
mysqli_select_db($link, "media");
if(isset($_POST['pictureID']))
{
$imgId = $_POST['pictureID'];
//echo $imgId; nothing else expect image should be echoed
if (!empty($imgId)) {
$sqliCommand = "SELECT Image FROM images WHERE Picture = $imgId";// select Image here so not select picture as you did earlier
$result = mysqli_query($link, $sqliCommand);
$row = mysqli_fetch_assoc($result);
mysqli_close($link);//close mysqli as script gonna exit after echoing image.
header("Content-type: image/jpeg");
echo $row['Image'];
exit();// this is important
}
}
?>
<html>
<body>
<!-- rest of the code -->
阅读我在代码中写的评论。
如果将标题设置为图像类型,则页面上的图像二进制代码必须没有预期。
如果问题仍然存在,请注释标题行(这将使页面以文本形式显示)并查看页面中是否还有其他内容需要二进制形式的图像数据。