有人可以请帮忙,因为我不知所措。如何从MYSQL数据库中获取图像到我的网页上。 anyname.html
数据库名称是:upload 表名是:images
id Primary int(11)AUTO_INCREMENT
name varchar(100)
size int(11)
输入varchar(20)
content mediumblob
上传效果很好但是无法让它显示能够在html页面中使用它的图像。当我上传图片后,我得到了一个回复:对于存储的11号图像,但我看不到图像:这是我的代码,所以请,请有人帮忙,因为我已经多年没有结果了。
我的upload.php代码:
<?php
// Check for post data.
if ($_POST && !empty($_FILES)) {
$formOk = true;
//Assign Variables
$path = $_FILES['image']['tmp_name'];
$name = $_FILES['image']['name'];
$size = $_FILES['image']['size'];
$type = $_FILES['image']['type'];
if ($_FILES['image']['error'] || !is_uploaded_file($path)) {
$formOk = false;
echo "Error: Error in uploading file. Please try again.";
}
//check file extension
if ($formOk && !in_array($type, array('image/png', 'image/x-png', 'image/jpeg', 'image/pjpeg', 'image/jpg', 'image/gif'))) {
$formOk = false;
echo "Error: Unsupported file extension. Supported extensions are JPG / PNG.";
}
// check for file size.
if ($formOk && filesize($path) > 500000) {
$formOk = false;
echo "Error: File size must be less than 500 KB.";
}
if ($formOk) {
// read file contents
$content = file_get_contents($path);
//connect to mysql database
if ($conn = mysqli_connect('localhost', 'root', '', 'upload')) {
$content = mysqli_real_escape_string($conn, $content);
$sql = "insert into images (name, size, type, content) values ('{$name}', '{$size}', '{$type}', '{$content}')";
if (mysqli_query($conn, $sql)) {
$uploadOk = true;
$imageId = mysqli_insert_id($conn);
} else {
echo "Error: Could not save the data to mysql database. Please try again.";
}
mysqli_close($conn);
} else {
echo "Error: Could not connect to mysql database. Please try again.";
}
}
}
?>
<html>
<head>
<title>Upload image to mysql database.</title>
<style type="text/css">
img{
margin: .2em;
border: 1px solid #555;
padding: .2em;
vertical-align: top;
}
</style>
</head>
<body>
<?php if (!empty($uploadOk)): ?>
<div>
<h3>Your Image has been Uploaded:</h3>
</div>
<div>
<img src="image.php?id=<?=$imageId ?>" width="300px" height="300px"></img>
<strong><br /><br />Embed</strong>: <input size="30" value='<img src="image.php?id=<?=$imageId ?>">'>
</div>
<hr>
<?php endif; ?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data" >
<div>
<h3>Image Upload:</h3>
</div>
<div>
<label>IMAGE SELECT<br /></label>
<input type="hidden" name="MAX_FILE_SIZE" value="500000">
<input type="file" name="image" />
<br /><br />
<input name="submit" type="submit" value="Upload Image">
</div>
</form>
</body>
</html>
答案 0 :(得分:0)
这似乎是PHP display image BLOB from MySQL
问题的重复希望它有所帮助。
答案 1 :(得分:0)
如果将图像数据存储在数据库中,请使用下一个image.php:
<?php
$id = $_GET['id'];
$link = mysql_connect('localhost', 'root', '');
mysql_select_db('upload');
$sql = "SELECT content FROM images WHERE id=$id";
$result = mysql_query("$sql");
$row = mysql_fetch_assoc($result);
mysql_close($link);
header("Content-type: image/jpeg");
echo $row['content'];
?>