在将其上传到数据库(PHP和Mysql)后无法显示图像

编程入门 行业动态 更新时间:2024-10-10 05:16:42
本文介绍了在将其上传到数据库(PHP和Mysql)后无法显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要为客户端创建一个网页,以将图像上传到数据库并显示。

I need to do a web page for a client to upload images to a data base and display them.

我实现将图片上传到数据库,但我无法显示它们,但我无法解决为什么

I am achieve to upload the images into a database, but I'm having trouble displaying them, but I can't work out why

这是我的代码:

<!DOCTYPE html> <head> <body> <form action="form.php" method="post" enctype="multipart/form-data"> File: <input type="file" name="image" /> <input type="submit" value="Upload" /> </form> <?php mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("test" ) or die(mysql_error()); $file = $_FILES['image'] ['tmp_name']; if (!isset($file)) { echo "<br>Please select an image."; } else { $image = addslashes(file_get_contents($_FILES['image'] ['tmp_name'])); $imageName = addslashes($_FILES['image']['name']); $imageSize = getimagesize($_FILES['image']['tmp_name']); if ($imageSize == FALSE) echo "<br><br>Thats not an image. <br><br>"; else{ if (!$insert = mysql_query("INSERT INTO imgup VALUES ('','$imageName','$image')")) echo "Problem uploading the image."; else{ $lastId = mysql_insert_id(); echo "Article uploaded.<p /> Your image:<p /> <img src=get.php?id=$lastId>"; } } } ?> </body> </html>

这是我的文件,将图像blob转换为图像:

This is my file who turn the image blob into an image:

<?php mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("test" ) or die(mysql_error()); $id = addslashes($_REQUEST['id']); $image = mysql_query("SELECT * FROM blog WHERE id=$id"); $image = mysql_fetch_assoc($image); $image = $image['image']; header("Content-type: image/jpeg"); echo $image; ?>

最后图片不显示,这是我得到的: goo.gl/gi1Uuc

And at the end the image does not display and this is what i get: goo.gl/gi1Uuc

如果我去查看我的数据库,映像已成功上传...

And if i go and check my database, the image has ben successfully uploaded...

推荐答案

根据文件使用内联base64编码。这是通过:

Depending on the file use inline base64 encoding. This is done with:

echo '<img src="data:image/jpeg;base64,'.base64_encode( $image ).'"/>';

Font: base64_encode

放置 T upperCase(Type),因为可能在IE中给出错误。尝试使用函数file_get_contents打印。

Put the T upperCase (Type), because can giving error in IE. Try printing with the function file_get_contents.

header('Content-Type: image/jpeg'); echo readfile($image);

更多推荐

在将其上传到数据库(PHP和Mysql)后无法显示图像

本文发布于:2023-06-02 01:27:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/430959.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:其上   图像   数据库   PHP   Mysql

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!