Insert image into database

Asked

Viewed 21,331 times

1

What type of variable do I use to store an image in the database? And is there a specific command for this? or just enter as any record?

I have a product table with the fields:

code, name, price, image, in the image I intend to store a product photo.

  • 2

    Use type BLOB, it is not a variable, it is the type of the field. It would be good for you to say which DBMS you use, and if possible post some code and/or leave your question more specific.

  • 2

    Which programming language you intend to use brother?

  • 2

    Inform the programming language, the database you want to record this information, because, everything are important variables for a good answer!???????????

  • 2

    @Harrypotter true, and even because of that there were complications in the answers below.

  • 2

    The truth is that the answers took to the head because of the question that should have been closed before there were answers.

  • 1

    The question is good but it should have been better explained, because the answers change according to whether the application is web or desktop, I think if it changes from to keep it active yet.

  • Yeah, but while it’s out, it’s on hold.

  • 2

    @Silvioandorinha the question is almost good, if the author had explained what he wants clearly would be good the question, but the way it is is almost impossible to answer. I was surprised with the attempts of answers being that I could not understand what the author wanted to ask.

  • @Amazing math is no one having closed the question soon.

  • 2

    @Jorgeb. well, I usually wait a little to close, I left my comment and I hoped that the author better, however were appearing answers, maybe the best would have been to close even and if the op better reopened from there

  • 2

    @Math also think, I start to agree with the closing soon, otherwise it gives wings to these kind of questions until seem interesting and is ruined.

  • Anderson, o Meta é a parte do site onde a gente dicute como funciona o SOPT. Your question was quoted in the comments to this post. Can you give your opinion there? And if you have any questions and want to clarify something do not hesitate to open a question there in [meta].

Show 7 more comments

3 answers

9


as already mentioned, one can use BLOB and LONGBLOB, but DON’T DO THIS.

There are several and diverse temptations whether to store images in databases, such as:

TEMPTATIONS - backup facility - single and uniform access between multiple nodes - sql image listing and accounting

however, we have the true heels of Quiles:

REALITY - backup becomes bulky beyond normal - Database server memory load is overloaded with non-related data - Bank server network traffic volume increases substantially

I suggest scheming an intelligent way of organizing data in an intuitive data structure, for example: /version/year/id-product/photos

If you want to perform data centralization and single point access, try distributed or replicated filesystems, there are thousands of alternatives (GFS,GPFS,AFS,DFS mogileFS, extremfs)

And there are also the document bases, where today many have distorted and become NOSQL (S3, couchdb, mongoDB, etc..)

3

It is difficult to help you without knowing which DBMS and which language you are using.
However, for example, I will use Mysql+PHP.

Answering your question: Mysql uses a LONGBLOB field.

Now, I believe is not a good practice. Change your application and do not do this.
Whether you’re in DBMS or not, your image will be on disk.
This will only make your database more "swollen"
Record in DBMS only the path to the image, will get lighter.

To upload images and save only the path to the database. You can read the php documentation on the subject here (link).

First you need 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>

Note that the "action" of the form points to "save.php". See how it turned out:

<?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.
}
?>
  • 2

    As a matter of fact these days I saw a article from microsoft on this on SOEN, which according to the study if the image is less than 256K that the BD has wide advantage, if it is greater than 1M the file system has wide advantage. Although it is the comparison Sqlserver vs NTFS, I think you can get an idea.

  • @Jorgeb. is an excellent article to translate and post as question/answer (logical, summarize the content). The source is reliable.

1

You can save the image path, where it is in 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 haven’t mentioned any in the question I leave it in php as an example here

  • 1

    This does not properly insert the image into the database.

  • @Renan no but I presented a practical solution, because when I did not know how to upload I also thought that the image was stored in the database and maybe the author of the question does not know it yet.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.