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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2858197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 16:05:00  来源:igfitidea点击:

Can i insert an image in a MYSQL DB table?

mysqlphpmyadmin

提问by kasrsf

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? :)

有没有办法将图片(不是 url,图片)插入到用 phpmyadmin 制作的 MYSQL 表中?如果我想获取那张图片并将其插入页面,我该怎么办?:)

回答by Eric Petroelje

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.

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

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.

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

  2. 如果您有使用select *.

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

  4. 您可以在提供图像的位置上获得一些灵活性 - 例如,如果您希望将来将它们移动到 CDN。

回答by aioobe

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).
  • 存储单个文件正是文件系统所面向的,因此它很可能会更好地扩展。
  • 更容易手动调试/查看图像
  • Web 服务器可以直接提供图像(不需要额外的代码来从数据库加载内容并提供适当的 mime 类型等)。

Personally I've always found that the benefits outweighs the drawbacks and always had the images (along with mime-types) in the database.

就我个人而言,我一直发现利大于弊,并且数据库中总是有图像(以及 mime 类型)。

Related questions and resources:

相关问题和资源:

回答by Matchu

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.

否则,将图像文件的内容存储在 BLOB 列中,然后<img src="image.php?id=1234" />在 HTML 中打印。

image.phpshould 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');

image.php应该在数据库中查找 ID 为 1234 的图像,获取该 BLOB 列的内容,并在提供正确的 HTTP 标头后打印它,例如 header('Content-type: image/gif');

But seriously. Just save the image file. It's much less of a pain, I promise.

不过实话说。只需保存图像文件。我保证不会那么痛。