我可以在 MYSQL DB 表中插入图像吗?

发布于 2024-09-02 03:58:10 字数 81 浏览 1 评论 0原文

有没有办法将图片(不是网址,图片)插入到用 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 技术交流群。

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

发布评论

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

评论(3

新雨望断虹 2024-09-09 03:58:11

如果您经常获取图像,则最好将其存储为文件并保存其路径,以节省获取图像时的数据库运行时间。

否则,将图像文件的内容存储在 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.

情感失落者 2024-09-09 03:58:10

这是可能的(使用 BLOB 类型),但最好将图像存储在文件系统中,并将图像的路径存储在数据库中。

我想到的几个原因是:

  1. BLOB 会增大数据库的大小,使备份和恢复可能变得更加困难(尽管有些人可能会认为这会让事情变得更容易,因为您只需备份数据库而不是比数据库加一堆文件)。

  2. 如果您有使用 select * 的惰性代码,将数据存储为 BLOB 可能会导致性能问题。

    如果

  3. 从文件系统提供图像将比从数据库提供图像更快,并减少数据库服务器上的负载。

    从文件系统提供图像将比从数据库

  4. 您可以在提供图像的位置上获得一定的灵活性 - 例如,如果您希望将来将它们移动到 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:

  1. 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).

  2. Storing the data as BLOBs could cause performance issues if you have lazy code that uses select *.

  3. Serving the images from the filesystem will be faster than serving them from the database, and reduce load on the db server.

  4. You get some flexibility with where you serve images - for example if you want to move them to a CDN in the future.

在风中等你 2024-09-09 03:58:10

当涉及到静态资源(例如背景图像或菜单图标等)时,我从未见过有人将它们存储在数据库中。对于动态内容(例如用户上传的图像),这种情况并不少见。

以下是在数据库中存储图像的一些优点和缺点。

优点

  • 从技术上讲,它与其他数据没有什么不同。只是更多,并且是二进制格式。
  • 图像与数据库的其余部分一起备份。
  • 不会因为不同步而头疼(数据库中的路径指向不存在的图像,或数据库中未引用的图像)

缺点:

  • 存储单个文件正是文件系统所适合的朝着,所以它很可能会更好地扩展。
  • 更容易手动调试/查看图像
  • Web 服务器可以直接提供图像(无需额外的代码来从数据库加载内容并提供适当的 mime 类型等)。

就我个人而言,我总是发现好处多于缺点,并且数据库中总是有图像(以及 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:

  • It's technically no different from other data. Just more, and in binary format.
  • Images are backed up along with the rest of the database.
  • No head ache of getting out of sync (having a path in the database pointing to a non-existent image, or unreferenced images in the database)

Drawbacks:

  • Storing individual files is precisely what the file system is geared towards, so it will most likely scale better.
  • Easier to manually debug / view the images
  • Web server can serve the images directly (no need for extra code to load content from database and serve proper mime type etc).

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:

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