有人可以請幫忙,因爲我無所適從。我如何從MYSQL數據庫獲取圖像到我的網頁上。 anyname.html如何在我的網頁上顯示存儲在我的mysql數據庫中的圖像文件?
數據庫名稱是:上傳 表名是:圖像
ID主INT(11)AUTO_INCREMENT
名稱VARCHAR(100)
大小INT(11)
類型varchar(20)
content mediumblob
上傳我和rks很棒,但不能讓它顯示能夠在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>
不要在數據庫中保存的文件。 http://stackoverflow.com/a/38829952/267540 – e4c5
爲什麼你首先將圖像數據放入數據庫?這通常更好地保存在文件系統中。既然你調用image.php來顯示圖像,你不覺得_that_腳本的代碼可能更相關...?無論如何,請直接在瀏覽器中使用相同的參數調用該腳本(並在之前註釋掉該腳本中的任何'header'調用),然後檢查結果。 – CBroe