php
include("connect_to_database.php");
if( !isset($_FILES["image"]) ){
echo "upload the file";
}else{
$image = mysql_query("SELECT * FROM upload WHERE id=1");
$image = mysql_fetch_assoc($image);
$image = $image["image"];
echo $image;
}
html
<form action="newindex.php" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit" value="Upload" >
</form>
上面的代码显示了相应id的图像,该图像已经以BLOB格式上传到mysql中。
现在,我需要显示最后 6 个 id 的图像或数据库中的所有图像。我已尝试以下代码,但它不起作用,而是显示 1 个图像。
php
include("connect_to_database.php");<br>
$query = mysql_query("SELECT * FROM upload ORDER BY id DESC");
while( $rows = mysql_fetch_array($query) ){
$image = $rows["image"];
header("Content-type: image/jpeg");
echo $image."<br/>";
}
最佳答案
您不应将多个图像输出到同一响应中。 Web 浏览器不会期望/理解第一个图像之后的另一个图像,就像将两个 jpg 文件复制到同一文件中无法在桌面上工作一样。
相反,创建另一个包含所有图像链接的(动态)页面 ( <img src="img.php?id=3">
)。然后,网络浏览器将一次请求一张图像。
$id = (int)$_REQUEST['id'];
$query = mysql_query("SELECT * FROM upload WHERE id=$id");
(请注意,与上面类似的代码确实不安全,永远不要在任何公共(public)环境中的 SQL 查询中直接使用输入值。我还建议使用 PDO 来执行查询)。
关于php - 通过PHP以mysql BLOB类型存储图像,需要一些帮助来显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14684432/