-1

Possible Duplicate:
Storing Images in DB - Yea or Nay?

I'm learning PHP and MySQL concepts and while I have a solid, basic understanding of both, I've still yet to put them into practical use and make them interact so forgive me if the question is a little silly.

I was just wondering if say, I had a table for articles that have cover images - would it make sense to have a column like 'imagepath' where I would store the path of this image and retrieve it when necessary to display the cover?

If not using the method above, what solution would I need to relate images to database related content?

Community
  • 1
  • 1
styke
  • 2,116
  • 2
  • 23
  • 51
  • 4
    Yup, sounds solid. See also [Storing Images in DB - Yea or Nay?](http://stackoverflow.com/q/3748) – Pekka Aug 12 '12 at 15:00
  • That would be somewhat unorthodox, in my opinion at least – A Person Aug 12 '12 at 15:00
  • Well, there isn't an accepted solution for everything, everything has its pros and cons, but this should work too – A Person Aug 12 '12 at 15:05
  • 1
    this is not the search box. Use the search box instead of the question box to put that kind of input into this website first. – hakre Aug 12 '12 at 15:05
  • @hakra I appreciate your discontent with this style of question, it's a little too late to delete it now. I find it slightly difficult to differentiate what's acceptable. Just a matter of time before it gets closed. – styke Aug 12 '12 at 15:07
  • And actucally the title is really worse. – hakre Aug 12 '12 at 15:08
  • 1
    Might want to update the question title to something a little less generic than "is this a good practice". – David Archer Aug 12 '12 at 15:08
  • http://programmers.stackexchange.com/ – dead beef Aug 12 '12 at 15:09
  • I'm fine with the content of the question. It's a legit software design question and we can objectively talk about the alternatives. Not seeing this as a subjective question (i.e. "what is the best way...") – David Archer Aug 12 '12 at 15:11

3 Answers3

4

Yes, that's how usually people do it. Storing images in a database like MySQL is not very efficient.

piokuc
  • 25,594
  • 11
  • 72
  • 102
  • It's amazing how easy it is to earn some points here sometimes. Other times you put lots of effort, write something really clever, and nobody gives a shit... – piokuc Aug 12 '12 at 15:22
1

Yes, storing a text string like the file path to reference the image stored elsewhere is a legitimate design decision. An alternative would be to store the raw image data into a blob column in the database, have a PHP script serve that image up, and you can reference that PHP script in your HTML IMG tag. There are pro's and con's to both scenarios.

David Archer
  • 2,121
  • 1
  • 15
  • 26
1

I think that's fine. The openlibrary website serves covers without the extra "path" indirection. All books on the site have a unique key identifying them and the covers are named with the same key name so that you can "compute" the path to the image rather than look it up. An example book is at http://openlibrary.org/books/OL20426778M/B%C3%A9atrix. and the corresponding cover is at http://covers.openlibrary.org/b/olid/OL20426778M-L.jpg. Notice the OL20426778M which is the identifier for the book.

I'm not sure what advantages the database approach offers but with this, you can tar/zip your images and offer bulk downloads if necessary etc.

Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169