【发布时间】:2013-08-09 14:03:22
【问题描述】:
我想知道如何在 php 文档上显示多张来自数据库的图片。我知道将图片放入 mysql 并取出其中一个,但我不知道如何取出多个(或全部)。提前致谢。
我的一个 php 文件看起来像这样
<html>
<head>
<title>Upload</title>
</head>
<form action='pictureuploadtest.php' method='POST' enctype='multipart/form-data'>
File: <input type='file' name='fileone'>
<input type = 'submit' name='submitfile'>
</form>
<?php
$con = mysql_connect("localhost", "username", "password") or die("Cannot connect: ".mysql_error());
mysql_select_db("testpicture") or die("Cannot connect to db: ".mysql_error());
$file = $_FILES['fileone']['tmp_name'];
if(!isset($file)) {
print "Choose an image";
} else {
$image = addslashes(file_get_contents($_FILES['fileone']['tmp_name']));
$imagename = addslashes($_FILES['fileone']['name']);
$imagesize = getimagesize($_FILES['fileone']['tmp_name']);
if($imagesize === false) {
echo "Invalid image.";
} else {
$insert = "INSERT INTO upload VALUES ('', '$imagename', '$image')";
if(mysql_query($insert, $con)) {
$lastid = mysql_insert_id();
echo "Image uploaded. <p /> Your image: <p /> <img src=getpic.php?id=$lastid width='300px' height='300px'>";
} else {
echo "Cannot upload image: ".mysql_error();
}
}
}
?>
</html>
然后getpic.php看起来像这样
<?php
mysql_connect("localhost", "username", "password") or die("Cannot connect: ".mysql_error());
mysql_select_db("testpicture") or die("Cannot connect to db: ".mysql_error());
$id = addslashes($_REQUEST['id']);
$image = mysql_query("SELECT * FROM upload WHERE id=$id");
$image = mysql_fetch_assoc($image);
$image = $image['image'];
echo $image;
?>
所以这段代码可以告诉用户上传一张图片,然后在将其添加到数据库后显示该图片,但是我如何在数据库中显示多张或所有图片。
提前致谢。
【问题讨论】:
-
帮自己一个忙,不要将图像放在数据库中,只是它们的名称\路径
-
这是正确的:不要将图像存储在数据库中。但我在下面为您的问题提供了技术答案:)
标签: php html mysql database image