php 我可以在 MySQL 中存储图像吗

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

Can I store images in MySQL

phpmysqlimage

提问by afaolek

Possible Duplicates:
Images in MySQL
Storing images in MySQL

可能的重复:
MySQL 中的图像 在 MySQL 中
存储图像

I'm trying to develop a website where users upload their images as part of registration. I want it that for each image, there should be a thumb created with PHP (which is not that difficult). I want to save the thumbs (since they are very small) in the database and I use MySQL. (I don't want to save the thumbs as physical files on the drive.)
Does MySQL allow saving and retrieving image data and how do I go about it? If it doesn't support image data, is there any free database that does? I will be happy if a link can be provided.
Thanks.

我正在尝试开发一个网站,用户可以在其中上传他们的图片作为注册的一部分。我希望对于每个图像,都应该有一个用 PHP 创建的拇指(这并不难)。我想在数据库中保存拇指(因为它们非常小)并且我使用 MySQL。(我不想将拇指保存为驱动器上的物理文件。)
MySQL 是否允许保存和检索图像数据,我该如何处理?如果它不支持图像数据,是否有任何免费的数据库支持?如果可以提供链接,我会很高兴。
谢谢。

回答by FinalForm

Yes, you can store images in the database, but it's not advisable in my opinion, and it's not general practice.

是的,您可以将图像存储在数据库中,但在我看来这是不可取的,也不是一般做法。

A general practice is to store images in directories on the file system and store references to the images in the database. e.g. path to the image,the image name, etc.. Or alternatively, you may even store images on a content delivery network (CDN) or numerous hosts across some great expanse of physical territory, and store references to access those resources in the database.

通常的做法是将图像存储在文件系统的目录中,并将对图像的引用存储在数据库中。例如,图像的路径、图像名称等。或者,您甚至可以将图像存储在内容交付网络 (CDN) 或跨越一些广阔物理区域的众多主机上,并存储引用以访问数据库中的这些资源.

Images can get quite large, greater than 1MB. And so storing images in a database can potentially put unnecessary load on your database and the network between your database and your web server if they're on different hosts.

图像可以变得非常大,大于 1MB。因此,将图像存储在数据库中可能会给您的数据库以及数据库和 Web 服务器之间的网络带来不必要的负载(如果它们位于不同的主机上)。

I've worked at startups, mid-size companies and large technology companies with 400K+ employees. In my 13 years of professional experience, I've never seen anyone store images in a database. I say this to support the statement it is an uncommon practice.

我曾在拥有 40 万多名员工的初创公司、中型公司和大型科技公司工作过。在我 13 年的专业经验中,我从未见过有人将图像存储在数据库中。我这样说是为了支持这种说法,这是一种不常见的做法。

回答by developerdoug

You'll need to save as a blob, LONGBLOB datatype in mysql will work.

您需要保存为 blob,mysql 中的 LONGBLOB 数据类型将起作用。

Ex:

前任:

CREATE TABLE 'test'.'pic' (
    'idpic' INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
    'caption' VARCHAR(45) NOT NULL,
    'img' LONGBLOB NOT NULL,
  PRIMARY KEY ('idpic')
)

As others have said, its a bad practice but it can be done. Not sure if this code would scale well, though.

正如其他人所说,这是一种不好的做法,但可以做到。不过,不确定此代码是否可以很好地扩展。

回答by George Cummins

You can store images in MySQL as blobs. However, this is problematic for a couple of reasons:

您可以将图像作为 blob 存储在 MySQL 中。但是,由于以下几个原因,这是有问题的:

  • The images can be harder to manipulate: you must first retrieve them from the database before bulk operations can be performed.
  • Except in very rare cases where the entire database is stored in RAM, MySQL databases are ultimately stored on disk. This means that your DB images are converted to blobs, inserted into a database, and then stored on disk; you can save a lot of overhead by simply storing them on disk.
  • 图像可能更难操作:您必须先从数据库中检索它们,然后才能执行批量操作。
  • 除了在极少数情况下整个数据库存储在 RAM 中的情况外,MySQL 数据库最终存储在磁盘上。这意味着您的数据库图像被转换为​​ blob,插入到数据库中,然后存储在磁盘上;您可以通过简单地将它们存储在磁盘上来节省大量开销。

Instead, consider updating your table to add an image_path field. For example:

相反,请考虑更新您的表以添加 image_path 字段。例如:

ALTER TABLE `your_table`
ADD COLUMN `image_path` varchar(1024)

Then store your images on disk, and update the table with the image path. When you need to use the images, retrieve them from disk using the path specified.

然后将您的图像存储在磁盘上,并使用图像路径更新表。当您需要使用图像时,请使用指定的路径从磁盘中检索它们。

An advantageous side-effect of this approach is that the images do not necessarily be stored on disk; you could just as easily store a URL instead of an image path, and retrieve images from any internet-connected location.

这种方法的一个有利的副作用是图像不一定存储在磁盘上。您可以轻松地存储 URL 而不是图像路径,并从任何连接 Internet 的位置检索图像。

回答by bsimic

You will need to store the image in the database as a BLOB.

您需要将图像作为 BLOB 存储在数据库中。

you will want to create a column called PHOTO in your table and set it as a mediumblob.

您需要在表中创建一个名为 PHOTO 的列并将其设置为 mediumblob。

Then you will want to get it from the form like so:

然后你会想要从表单中获取它,如下所示:

    $data = file_get_contents($_FILES['photo']['tmp_name']);

and then set the column to the value in $data.

然后将该列设置为 $data 中的值。

Of course, this is bad practice and you would probably want to store the file on the system with a name that corresponds to the users account.

当然,这是不好的做法,您可能希望使用与用户帐户对应的名称将文件存储在系统上。