[basebase解码后php图像mysql无法正确显示

问题描述 投票:0回答:1

我正在尝试将图像上传到mySQL数据库中,然后将其检索为注释。但是由于某种原因,图像无法正确显示(有时仅显示其中的一部分),所以我想我做错了什么。帮助将不胜感激。

kritiek.php

表格

<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>

post_comment.php

<?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);

mySQL数据库

Screenshot of tablesScreenshot of table content

php html
1个回答
0
投票

发现它加载不正确的问题,问题是将mySQL行设置为blob而不是longblob,所有图像的最大大小均为63.9 kb,因此很明显是错误的。

© www.soinside.com 2019 - 2024. All rights reserved.