我正在尝试将图像上传到mySQL数据库,然后将其检索到注释中。但是由于某些原因,图像无法正确显示(有时只是部分图像)所以我想我做错了。帮助将不胜感激。
表格
<form action="post_comment.php" method="POST" id="post-comment" enctype="multipart/form-data">
<fieldset>
<legend>Reactie plaatsen.</legend>
<input type="text" name="name" required placeholder=" Naam">
<br>
<input type="text" name="captcha" id="commentCaptcha" placeholder=" captcha">
<br>
<textarea name="comment" cols="50" rows="4" maxlength="999" required placeholder=" Laat een reactie achter.."></textarea>
<br>
Profiel Foto: <input type="file" name="image" accept="image/*">
<br><br>
<input type="submit" value="Add comment">
</fieldset>
</form>
获取评论
<section id="comments_section">
<?php
$con = mysqli_connect("Host","Username","Password","Database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "SELECT * FROM Comments_table";
$result = mysqli_query($con, $query) or die("Error: ".mysqli_error($con));
while($row = mysqli_fetch_array($result)) {
$img=base64_encode($row['image']);
$date = $row['date'];
?>
<div class="comment">
<?php
echo "<br>" . displayImage($img) . "<br>" . $row['name'] . "<br><br>" . $row['comments'] . "<br><br>" . $date . "<br><br>";
?>
</div>
<?php
}
mysqli_close($con);
function displayImage(&$link){
if(!empty($link)){
return "<img alt=\"Profile Picture\" src=\"data:image/*;charset=utf8;base64, $link \">";
}
else
{
return "<img src='img/nopicture.jpg'>";
}
}
?>
</section>
<?php
$con = mysqli_connect("Host","Username","Password","Database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//assign form to variables
$name = mysqli_real_escape_string($con, strip_tags($_POST["name"]));
$comment = mysqli_real_escape_string($con, strip_tags($_POST["comment"]));
$comment_length = strlen($comment);
$unix_time = time();
$mySQL_date = date( 'Y-m-d', $unix_time);
// Make sure the user uploaded a file
if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) {
// Temporary file name stored on the server
$tmpName = $_FILES['image']['tmp_name'];
// Read the file
$fp = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
$data = addslashes($data);
fclose($fp);
}
mysqli_query($con,"INSERT INTO Comments_table VALUES('','$name','$comment','$data','$mySQL_date')");
header("location: kritiek.php");
mysql_close($con);
答案 0 :(得分:0)
wohoo~发现它加载不当的问题,问题是mySQL行设置为blob而不是longblob,所有图像的最大大小都精确到63.9 kb所以很明显出了什么问题。