我可以在 MYSQL DB 表中插入图像吗?
有没有办法将图片(不是网址,图片)插入到用 phpmyadmin 制作的 MYSQL 表中? 如果我想获取该图片并将其插入页面中,我该怎么办? :)
is there a way to insert pics(not url,the pic)into a MYSQL table made with phpmyadmin?
and if there is when i want to get that picture and insert it in the page , what should i do? :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您经常获取图像,则最好将其存储为文件并保存其路径,以节省获取图像时的数据库运行时间。
否则,将图像文件的内容存储在 BLOB 列中,然后在 HTML 中打印
。
image.php
应该在数据库中查找 ID 为 1234 的图像,获取该 BLOB 列的内容,并在提供正确的 HTTP 标头后打印它,例如header('Content-type : image/gif');
但说真的。只需保存图像文件即可。我保证,这会减轻很多痛苦。
If you'll be fetching the image often, you'd probably be better off storing it as a file and saving the path to it, to save you a database run on fetching the image.
Otherwise, store the content of the image file in a BLOB column, and then print
<img src="image.php?id=1234" />
in the HTML.image.php
should look up the image with ID 1234 in the database, fetch the contents of that BLOB column, and print it after serving the correct HTTP header, e.g.header('Content-type: image/gif');
But seriously. Just save the image file. It's much less of a pain, I promise.
这是可能的(使用 BLOB 类型),但最好将图像存储在文件系统中,并将图像的路径存储在数据库中。
我想到的几个原因是:
BLOB 会增大数据库的大小,使备份和恢复可能变得更加困难(尽管有些人可能会认为这会让事情变得更容易,因为您只需备份数据库而不是比数据库加一堆文件)。
如果您有使用
select *
的惰性代码,将数据存储为 BLOB 可能会导致性能问题。如果
从文件系统提供图像将比从数据库提供图像更快,并减少数据库服务器上的负载。
从文件系统提供图像将比从数据库
您可以在提供图像的位置上获得一定的灵活性 - 例如,如果您希望将来将它们移动到 CDN。
您可以在提供图像的位置上获得一定的灵活性 - 例如,
It is possible (using the BLOB type) but it is almost always better to just store the image in the filesystem and just store the path to the image in the database.
A few reasons why off the top of my head:
BLOBs will balloon the size of your database, making backups and restores potentially more difficult (although some might argue that it makes it easier since you only have to back up the database rather than the database plus a bunch of files).
Storing the data as BLOBs could cause performance issues if you have lazy code that uses
select *
.Serving the images from the filesystem will be faster than serving them from the database, and reduce load on the db server.
You get some flexibility with where you serve images - for example if you want to move them to a CDN in the future.
当涉及到静态资源(例如背景图像或菜单图标等)时,我从未见过有人将它们存储在数据库中。对于动态内容(例如用户上传的图像),这种情况并不少见。
以下是在数据库中存储图像的一些优点和缺点。
优点:
缺点:
就我个人而言,我总是发现好处多于缺点,并且数据库中总是有图像(以及 mime 类型)。
相关问题和资源:
When it comes to static resources such as background images or menu icons etc, I've never seen anyone store them in the database. For dynamic content such as user uploaded images, it's not that uncommon.
Here are some pro's and con's of storing images in the database.
Benefits:
Drawbacks:
Personally I've always found that the benefits outweighs the drawbacks and always had the images (along with mime-types) in the database.
Related questions and resources: