Как хранить картинки в БД MySQL?

Отдельное поле с указанием типа.
Или делать магию узнавания типа файла по магии php.net/manual/en/function.mime-content-type.php
Или (правильное решение) после загрузки файла приводить его к одному типу (жпг, пнг, вебм)
Или даже так https://stackoverflow.com/a/21732109
- Вконтакте
Сначала делаете таблицу для хранения картинок:
Для отображения картинки на веб странице:
- Вконтакте

кладут в отдельный файл image.php и обращаются по GET
но на всякий случай сообщу, что код, который вам написали выше использовать в таком виде нельзя. Подключение должно быть через PDO с bindParam, GET переменные должны экранироваться, нужно предусмотреть картинку-заглушку если айди не найден и т.д.
Как хранить картинки в базе данных mysql
Хранение изображений в базе данных MySQL
Для хранения изображений в базе данных MySQL необходимо определить одно из полей таблицы как производное от типа BLOB. Сокращение BLOB означает большой двоичный объект. Тип хранения данных BLOB обладает несколькими вариантами:
TINYBLOB — может хранить до 255 байт
BLOB — может хранить до 64 килобайт информации
MEDIUMBLOB — до 16 мегабайт
LONGBLOB — до 4 гигабайт
Соответсвенно, для хранения изображений нам надо создать таблицу images с двумя полями:
id — уникальный ID изображения
content — поле для хранения изображения
How to store pictures in MySQL database
This tutorial provides the steps required to store images in the MySQL database.
MySQL is among the popular database management systems. It can be used to store and retrieve data designed according to the applications. Most of the applications are required to manage the images. This tutorial provides the options to store images in a MySQL table.
Notes: Professionals can help you with image database development for your company or your project.
Store the Image Path
The simplest approach is to store images in directories on the file system and store references to the photos in the database like SQL & MySQL, for example, the path to the image, the image name, and so on. Alternatively, you may even keep images on a CDN or numerous hosts across some great expanse of substantial territory and keep their references to access those resources in that database.
In this way, we are not required to hold the entire image in the database but to store only the path to the image stored in a folder. Storing images is good if the images will always be in the same folder, i.e. if each image will have its unique path that will always be the same. However, in some cases, the images are to be transferred from one folder to another. In that case, the best solution is to create a dynamic path to the image file, so if the image has to be moved, there would be no obligation to modify the database.
We can update an existing table to add an image path field as shown below.
Another approach is to create a separate table for the images and add a reference in tables using the image table. In this way, multiple tables can store the images in a single table. You may use the below-mentioned table to store your images.
In the above-mentioned table, the name is the actual name of the image being stored, and the title is optional to get input from the users uploading the image. Also, the type column can decide whether the image is stored locally or on a CDN. If it’s stored locally, then we can use the column directory to specify the path where the image is stored. In the case of CDN, we can use the column URL to get the base path of the image. Apart from type, directory, and URL, we can also use small, medium, and large to store the same image in multiple sizes.
When you need to use the pictures, retrieve them from the disk using the path specified. This approach’s perk is that the pictures do not necessarily need to be stored on a disk; we can hold a URL instead of an image path and retrieve images from any internet-accessible location.
Store Image in MySQL Table
Another way to store an image in the MySQL database is to store it in the table itself. Images size can be quite big, sometimes bigger than 1 or 2MB. So, storing images in a database can put additional load on your database and the network between your database and your web server if they’re on separate hosts.
In this approach, the image files can be challenging to manage. You must first restore them from the database before other operations are to be performed.
There are some exceptions where the entire database is stored in RAM. MySQL databases are sequentially stored on a disk. This means that your database picture files are converted to blobs, embedded into a database, and then kept on a disk. We can avoid a lot of problems by simply storing them on a disk as mentioned in the first approach.
Now create the table Image to store the images in the database. I have used id to identify each image and caption to store the name of the image for display purposes. The picture column stores the image in the table using the data type as LONGBLOB.
Summary
This tutorial provided the options to store images using the MySQL database. The same approach can be followed for other databases.
Storing images in MySQL [closed]
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago .
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.
4 Answers 4
If the image is located on your MySQL host, you could use the LOAD_FILE() function to store the image in a BLOB field:
You have to make sure that the image file is readable by MySQL, and that the MySQL user has the FILE privilege. To grant the FILE privilege, log-in as root and execute:
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.
You could store them as a BLOB . if you want to write all the coding, encoding, checking, etc.
You can use a BLOB field 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.