有没有办法通过PHP显示上传到MySQL的图像?

发布于 2025-02-11 09:57:01 字数 4104 浏览 0 评论 0原文

我正在尝试显示从MySQL到HTML页面的图像,但我看到的只是图标或有时是某些字符。这就是我看到的。 显示了我看到的

图标输入字段。我的表格看起来像这样。

<form method="post" action="http://localhost:63342/sqlvali-master111/publicRoot/sqlvali/index.php?action=lec_hub/pages">
                    <div class="form-group">
                        <label for="description" class="control-label">Description:</label>
                        <input type="text" class="form-control" id="description" name="description">
                    </div>
                    <div class="form-group">
                        <label for="external_link" class="control-label">External Link:</label>
                        <input type="text" class="form-control" id="external_link" name="external_link">
                    </div>
                    <div class="form-group">
                        <label for="query_structure" class="control-label">Query Structure:</label>
                        <input type="file" class="form-control" id="query_structure" name="query_structure">
                    </div>
                    <div class="form-group">
                        <label for="feedback" class="control-label">Feedback:</label>
                        <input type="text" class="form-control" id="feedback" name="feedback">
                    </div>
                    <div class="form-group">
                        <label for="likert" class="control-label">Likert-type Scale:</label>
                        <input type="text" class="form-control" id="likert" name="likert">
                    </div>
                    <div class="form-group">
                        <label for="query_example" class="control-label">Query Example:</label>
                        <input type="text" class="form-control" id="query_example" name="query_example">
                    </div>
                    <div class="form-group">
                        <!--                        <label for="lec_id_fk" class="control-label">Lecture Foreign Key:</label>-->
                        <input type="hidden" class="form-control" id="chapter_id_fk" name="chapter_id_fk" value="<?php echo $chapter_id?>">
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        <input name = "add" type = "submit" id = "add" value = "Add">
                    </div>
                </form>

这就是我在MySQL中将表单数据添加到数据库中的方式,

<?php

$con = mysqli_connect("localhost", "root", "", "lectureHub");
if(isset($_POST['add'])) {
    $description = $_POST['description'];
    $external_link = $_POST['external_link'];
    $chapter_id_fk = $_POST['chapter_id_fk'];
    $query_structure = $_POST['query_structure'];
    $feedback = $_POST['feedback'];
    $likert = $_POST['likert'];
    $query_example = $_POST['query_example'];
    $sql = "INSERT INTO pages (description, external_link, chapter_id_fk, query_structure, feedback, likert, query_example) VALUES('$description','$external_link','$chapter_id_fk', '$query_structure', '$feedback', '$likert', '$query_example')";
//                        mysqli_select_db("lecturehub");
    $retval = mysqli_query($con, $sql);
    if(! $retval ) {
        die('Could not enter data: ' . mysqli_error());
    }
}

这是我当前尝试在HTML中显示图片的方式,这仅使我呈现图标。

<div class='col-md-8'>
                    <h3>query Structure</h3>
//                    <p>$query_structure</p>
                      <img src='data:image;base64,".base64_encode($row['query_structure'])."' alt='image' style='width: 100px; height: 100px'>        
                </div> 

I am trying to display an image from MySQL to an HTML page but all I see is an icon or sometimes some characters. This is what I see.
Shows icon which I see

I am using a form to upload the image to the database with some other input fields as well. My form looks like this.

<form method="post" action="http://localhost:63342/sqlvali-master111/publicRoot/sqlvali/index.php?action=lec_hub/pages">
                    <div class="form-group">
                        <label for="description" class="control-label">Description:</label>
                        <input type="text" class="form-control" id="description" name="description">
                    </div>
                    <div class="form-group">
                        <label for="external_link" class="control-label">External Link:</label>
                        <input type="text" class="form-control" id="external_link" name="external_link">
                    </div>
                    <div class="form-group">
                        <label for="query_structure" class="control-label">Query Structure:</label>
                        <input type="file" class="form-control" id="query_structure" name="query_structure">
                    </div>
                    <div class="form-group">
                        <label for="feedback" class="control-label">Feedback:</label>
                        <input type="text" class="form-control" id="feedback" name="feedback">
                    </div>
                    <div class="form-group">
                        <label for="likert" class="control-label">Likert-type Scale:</label>
                        <input type="text" class="form-control" id="likert" name="likert">
                    </div>
                    <div class="form-group">
                        <label for="query_example" class="control-label">Query Example:</label>
                        <input type="text" class="form-control" id="query_example" name="query_example">
                    </div>
                    <div class="form-group">
                        <!--                        <label for="lec_id_fk" class="control-label">Lecture Foreign Key:</label>-->
                        <input type="hidden" class="form-control" id="chapter_id_fk" name="chapter_id_fk" value="<?php echo $chapter_id?>">
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        <input name = "add" type = "submit" id = "add" value = "Add">
                    </div>
                </form>

This is how I add the form data to my database in MySQL

<?php

$con = mysqli_connect("localhost", "root", "", "lectureHub");
if(isset($_POST['add'])) {
    $description = $_POST['description'];
    $external_link = $_POST['external_link'];
    $chapter_id_fk = $_POST['chapter_id_fk'];
    $query_structure = $_POST['query_structure'];
    $feedback = $_POST['feedback'];
    $likert = $_POST['likert'];
    $query_example = $_POST['query_example'];
    $sql = "INSERT INTO pages (description, external_link, chapter_id_fk, query_structure, feedback, likert, query_example) VALUES('$description','$external_link','$chapter_id_fk', '$query_structure', '$feedback', '$likert', '$query_example')";
//                        mysqli_select_db("lecturehub");
    $retval = mysqli_query($con, $sql);
    if(! $retval ) {
        die('Could not enter data: ' . mysqli_error());
    }
}

This is my current try to display the picture in HTML which renders me only the Icon.

<div class='col-md-8'>
                    <h3>query Structure</h3>
//                    <p>$query_structure</p>
                      <img src='data:image;base64,".base64_encode($row['query_structure'])."' alt='image' style='width: 100px; height: 100px'>        
                </div> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多孤肩上扛 2025-02-18 09:57:01

不要将文件上传到数据库。
将文件存储在服务器上,例如Web根部的媒体文件夹。
然后将该图像的路径存储在数据库中。
它将使您的数据库更具弹性,并减少服务器上的负载以及服务器所需的RAM量。

另外,通过将其存储在Web根中,您可以利用浏览器缓存和Web服务器快速流式传输文件,而不是通过数据库引擎将其加载到DISP中,然后通过网络套接字传递到PHP,然后将其作为base64 Image渲染或将其传递给网络服务器的二进制图像,后者将其传递给客户端作为长二进制流。

当它在WebRoot中时,您会从客户端获取请求,并且Web服务器将文件直接从磁盘/自己的缓存传输到客户端,后者将文件存储在其浏览器缓存中,从而在后续访问时减轻服务器上的负载。

Don't upload files to a database.
Store the files on the server, in a media folder for example of your web root.
Then store the path to that image in your database.
It will make your database a lot more resilient and reduces the load on your server and the amount of RAM the server will need for caching.

Also, by storing it in the web root you can leverage browser caching and webserver quickly streaming a file, instead of loading it into ram from disc by the database engine, then passing it via network socket to php, then rendering it as a base64 image or a binary image, passing that to the webserver, who passes that to the client as a long binary stream.

When it's in the webroot you get a request from the client, and the webserver streams the files directly from disk/own cache to the client, who stores the file in their browser cache, lightening the load on your server on subsequent visits.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文