What kind of variable do I use to store an image in the database? And is there a specific command for this? or just insert as any record?
I have a product table with fields:
code, name, price, image, in the image I intend to store a product photo.
What kind of variable do I use to store an image in the database? And is there a specific command for this? or just insert as any record?
I have a product table with fields:
code, name, price, image, in the image I intend to store a product photo.
As already mentioned, you can use BLOB and LONGBLOB, but DO NOT DO .
There are many different temptations to store images in databases, such as:
TRIAL - Backup facility - single and uniform access between multiple nodes - listing and counting images per sql
However, we have the true Achilles' heels:
REALITY - backup becomes bulky beyond normal - Bank server memory load becomes overloaded with data that is not relatable per se - network server traffic volume increases substantially
I suggest schematizing an intelligent form of data organization in an intuitive data structure, eg: / version / year / product id / photos
If you want to perform a data centralization and single access point, try distributed or replicated filesystems, there are thousands of alternatives (GFS, GPFS, AFS, DFS mogileFS, extremfs)
And there are also the bases of documents, where today many have become distorted and become NOSQL (S3, CouchDB, mongoDB, etc.).
It is difficult to help you without knowing which DBMS and which language you are using. However, for example, I'm going to use MySQL + PHP.
Answering your question: In MySQL you use a field of type LONGBLOB.
Now, I believe is not good practice. Change your application and do not do it.
Regardless of whether you are in the DBMS or not, your image will be on disk.
This will only make your database get more "bloated"
Record in the DBMS only the path to the image, it will get lighter.
To upload images and save only the path in the database. You can read the php documentation on the subject here (link) .
First you need to have a form to select the image:
<form enctype="multipart/form-data" action="salvar.php" method="post">
Imagem: <input name="userfile" type="file" />
<input type="submit" value="Enviar" />
</form>
Notice that the "action" of the form points to "save.php". Here's how it went:
<?php
$uploaddir = '/upload/imagens/'; //directório onde será gravado a imagem
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir . $_FILES['userfile']['name'])) {
$uploadfile = $uploaddir . $_FILES['userfile']['name'];
//grava na base de dados, no campo imagem, somente o nome da imagem que ficou gravado na variável $uploadfile que criamos acima.
} else {
//não foi possível concluir o upload da imagem.
}
?>
You can save the image path where it is on your ftp for example:
foto_do_produto(varchar 100) - seu campo
/images/produtos/prod50156.png - o valor do campo
For more references and examples of other languages although you have not mentioned any in the question I leave it in php as an example here