在 MySQL 中存储图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3014578/
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:16:57  来源:igfitidea点击:

Storing images in MySQL

mysql

提问by Gautam Kumar

Please give me the query for inserting images in a MySQL database. I am new to stackoverflow, so please ignore me if my question is not up to mark.

请给我在 MySQL 数据库中插入图像的查询。我是 stackoverflow 的新手,所以如果我的问题不符合标准,请忽略我。

回答by Daniel Vassallo

If the image is located on your MySQL host, you could use the LOAD_FILE()function to store the image in a BLOBfield:

如果图像位于您的 MySQL 主机上,您可以使用该LOAD_FILE()函数将图像存储在一个BLOB字段中:

CREATE TABLE MyTable (id INT, image BLOB);

INSERT INTO MyTable (id, image) VALUES(1, LOAD_FILE('/tmp/your_image.png'));

You have to make sure that the image file is readable by MySQL, and that the MySQL user has the FILEprivilege. To grant the FILEprivilege, log-in as root and execute:

您必须确保 MySQL 可以读取图像文件,并且 MySQL 用户具有该FILE权限。要授予FILE权限,请以 root 身份登录并执行:

GRANT FILE ON *.* TO 'mysql_user'@'localhost';

回答by Selenia

Is there a particular reason why you can't store a reference to the image, rather than the actual image?

是否有特殊原因不能存储对图像的引用,而不是实际图像?

Images are big—they make databases big. SQL can be painful (as it's not object-oriented) and doesn't have an image type.

图像很大——它们使数据库变大。SQL 可能很痛苦(因为它不是面向对象的)并且没有图像类型。

You could store them as a BLOB... if you want to write all the coding, encoding, checking, etc.

BLOB如果您想编写所有编码、编码、检查等,您可以将它们存储为...

回答by Eric Petroelje

You can use a BLOBfield to do this, but I would generally not recommend that. It's almost always better to simply store the image on the filesystem and store the path to the image in the database.

您可以使用BLOB字段来执行此操作,但我通常不建议这样做。将图像简单地存储在文件系统上并将图像的路径存储在数据库中几乎总是更好。

ETA:

预计到达时间:

See this questionfor more discussion

请参阅此问题以进行更多讨论

回答by Prashant Gonga

It is always a better idea to store images in external folders and store the refrence in the database.

将图像存储在外部文件夹中并将参考存储在数据库中始终是一个更好的主意。

Here is the step by step guide to upload images in the database. http://www.phpriot.com/articles/images-in-mysql/7

这是在数据库中上传图像的分步指南。 http://www.phpriot.com/articles/images-in-mysql/7

http://forum.codecall.net/topic/40286-tutorial-storing-images-in-mysql-with-php/

http://forum.codecall.net/topic/40286-tutorial-storing-images-in-mysql-with-php/

Here is the link for step by step guide for retreival of images from the database http://forum.codecall.net/topic/40849-tutorial-storing-images-in-mysql-with-php-part-ii-display-your-images/

这是从数据库中检索图像的分步指南的链接 http://forum.codecall.net/topic/40849-tutorial-storing-images-in-mysql-with-php-part-ii-display-你的图片/

Some more information on BLOB http://forums.mysql.com/read.php?20,17671,27914

关于 BLOB 的更多信息 http://forums.mysql.com/read.php?20,17671,27914