我多年来一直在努力,检查谷歌的每一个可能的地方, 有时候这样做会让我觉得脑子死了。
我这样做是为了它的乐趣,但我仍然坚持使用BLOB从MySQL数据库显示图像这一特定方面。
据我所知,我认为我正在正确上传图像,但每当我尝试显示图像时,它最终只会给我一个默认的图像图标和图像的名称,然后当我检查源,它为图像提出了胡言乱语。
有人可以告诉我我做错了吗?
show_profile.php
<html>
<body>
<a href="main_page.php">View Records</a>
<a href="logout.php">Logout</a>
<div id="body_show">
<link href="show_profile.css" rel="stylesheet" type="text/css">
<div id="image" class="image" style="width: 152px; height:152px;">
<?php
include('database_connection.php');
if(session_id() ==""){
session_start();
}
$username = $_SESSION['myusername'];
$query = "SELECT * FROM $dImage_table WHERE username='$username' and imageID = (SELECT MAX(imageID) FROM $dImage_table WHERE username='$username')";
$result=mysqli_query($DBConn, $query);
if(!$result){
$message = "<p>
There was an error with the query.<br />\n" .
"The error was " .
htmlspecialchars(mysqli_error($DBConn), ENT_QUOTES) .
".<br />\nThe query was '" .
htmlspecialchars($query, ENT_QUOTES ) .
"'</P>\n";
}
else if (!mysqli_num_rows($result)){
$message = "<p>Cannot find Image in the database.</p>\n";
}
else{
while($report = mysqli_fetch_assoc($result)){
$imageData = $report['image'];
$imageName = $report['imageName'];
$imageType = $report['imageType'];
$imageID = $report['imageID'];
//echo "<img src="'data:image;base64,'.$imageData."' alt='Image'/>";
//echo "<img src=data:$imageType;base64,base64_encode($imageData) alt=$imageName width=152px height=152px/>";
echo '<img src="data:'.$imageType.';base64,'. base64_encode($imageData).'" alt="'.$imageName.'" width="152px" height="152px" />';
//echo "</br>";
}
//echo $imageData;
}
?>
</div>
<div class="form_body">
<form id="form_body" action="pic_uploader.php" method="POST" enctype="multipart/form-data">
<input type="file" name="image"><input type="submit" name="submit" value="Upload">
</form>
<div>
<?php
//echo $message;
?>
</div>
</body>
pic_uploader.php
<?php
include('database_connection.php');
session_start();
if(isset($_POST['submit']))
{
$username = $_SESSION['myusername'];
$imageName = mysqli_real_escape_string($DBConn, $_FILES["image"]["name"]);
$imageData = mysqli_real_escape_string($DBConn, file_get_contents($_FILES["image"]["tmp_name"]));
$imageType = mysqli_real_escape_string($DBConn, $_FILES["image"]["type"]);
$imageData= base64_encode($imageData);
if(!substr($imageType,0,4) == "image")
{
$message = "<p>Only Images are allowed!</p>";
include 'show_profile.php';
}
else{
$query = 'INSERT INTO dImage_table (username, imageID, imageName, imageType, image)
VALUES("'.$username.'",
"'. "" .'",
"'.$imageName.'",
"'.$imageType.'",
"'.$imageData.'")';
if(!mysqli_query($DBConn, $query))
{
$message = "<p>
There was an error uploading the image.<br />\n" .
"The error was " .
htmlspecialchars(mysqli_error($DBConn), ENT_QUOTES) .
".<br />\nThe query was '" .
htmlspecialchars($query, ENT_QUOTES ) .
"'</P>\n";
}
else
{
$message = "<p>Image uploaded</p>";
include 'show_profile.php';
}
}
}
?>
感谢您的输入
答案 0 :(得分:0)
将图像上传到Web服务器并存储图像的URL,从未见过任何人将图像直接存储到mysql数据库中。这是一个关于如何上传文件的简单教程:http://www.w3schools.com/php/php_file_upload.asp