PHP / MYSQL:将图像上传到具有base64编码的数据库

时间:2019-12-30 02:43:56

标签: php mysql tobase64string

当前,我开发了一个项目,要求用户从计算机上载图像。我是通过互联网上的示例进行此操作的。目前,数据库中的列图像使用“ BLOB”,并且上传成功。

但是,我不想使用BLOB,我想更改为LONGTEXT。当我更改此格式时,保存到数据库的图像成功,但是图像显示损坏。编码的文本和列图像也看起来很奇怪。我能知道是什么问题吗?下面是代码

    <?php  
    $connect = mysqli_connect("localhost", "root", "", "imagephp");  
    if(isset($_POST["insert"]))  
    {  
        $file = addslashes(file_get_contents($_FILES["image"]["tmp_name"]));  
        $query = "INSERT INTO tbl_images(name) VALUES ('$file')";  
        if(mysqli_query($connect, $query))  
        {  
            echo '<script>alert("Image Inserted into Database")</script>';  
        }  
    }  
    ?>  
    <!DOCTYPE html>  
    <html>  
        <head>  
            <title>Image Upload</title>  
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>  
            <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
            <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
        </head>  
        <body>  
            <br /><br />  
            <div class="container" style="width:500px;">  
                    <h3 align="center">Insert and Display Images From Mysql Database in PHP</h3>  
                    <br />  
                    <form method="post" enctype="multipart/form-data">  
                        <input type="file" name="image" id="image" />  
                        <br />  
                        <input type="submit" name="insert" id="insert" value="Insert" class="btn btn-info" />  
                    </form>  
                    <br />  
                    <br />  
                    <table class="table table-bordered">  
                        <tr>  
                            <th>Image</th>  
                        </tr>  
                    <?php  
                    $query = "SELECT * FROM tbl_images ORDER BY id DESC";  
                    $result = mysqli_query($connect, $query);  
                    while($row = mysqli_fetch_array($result))  
                    {  
                        echo '  
                            <tr>  
                                <td>  
                                        <img src="data:image/jpeg;base64,'.base64_encode($row['name'] ).'" height="200" width="200" class="img-thumnail" />  
                                </td>  
                            </tr>  
                        ';  
                    }  
                    ?>  
                    </table>  
            </div>  
        </body>  
    </html>  
    <script>  
    $(document).ready(function(){  
        $('#insert').click(function(){  
            var image_name = $('#image').val();  
            if(image_name == '')  
            {  
                    alert("Please Select Image");  
                    return false;  
            }  
            else  
            {  
                    var extension = $('#image').val().split('.').pop().toLowerCase();  
                    if(jQuery.inArray(extension, ['gif','png','jpg','jpeg']) == -1)  
                    {  
                        alert('Invalid Image File');  
                        $('#image').val('');  
                        return false;  
                    }  
            }  
        });  
    });  
    </script>  

希望有人可以帮助我。谢谢!

0 个答案:

没有答案