2

I'd like to know if it's faster/better to retrieve image(s) for a particular product or blog post or whatever in a website by getting the path/filename from the database along with the rest of the data, or if I should just create a folder with the product id and use glob to iterate over it or just use the id as filename in case of a single image, for example.

What are the advantages/disadvantages to these approaches?

Rob
  • 21
  • 1
  • You already know everything, Second method is easier to implement and first one is more flexible. Its only you who can judge what suits you best. As for the "speed" of the operations - it's E.Q.U.A.L – Your Common Sense Dec 21 '10 at 11:34
  • possible duplicate of [Saving images in database mysql](http://stackoverflow.com/questions/2753193/saving-images-in-database-mysql) – ajreal Dec 21 '10 at 11:38
  • ajreal - Not really. I'm not asking if I should store the file itself on the database. – Rob Dec 21 '10 at 11:40
  • @Col. Shrapnel - Thanks. Would you explain in what ways the first method is more flexible? I have a bad habit of not seeing these things and getting screwed in the future. – Rob Dec 21 '10 at 11:42
  • my bad, but is not able to undo – ajreal Dec 21 '10 at 11:46
  • To speed up things (ie. caching) you will probably end up storing the image on the filesystem anyway, so why even bother storing it in the DB? For most web applications the extra overhead involved (ie. converting BLOBs back to the original data) just isn't worth it. BTW Etags also might not work when using BLOBs. – wimvds Dec 21 '10 at 13:07
  • @wimvds That wasn't my question at all. I never said anything about blobs, just storing the path and filename on the database. – Rob Dec 21 '10 at 13:09
  • @Rob: Oops! I apparently misread your question... Must be the weather (freezing cold outside, hotter than hell inside, getting sleepy) :p. – wimvds Dec 21 '10 at 14:36

3 Answers3

0

It's definitely faster to use the paths/name methods (- 1 database calls; composing a string is not a time/memory consuming operation). By the way, in case you'll have different images for the same product id, you might create a foreach loop that looks in the given folder for pic1.png, pic2.png [...] picn.png.

There is a problem, though: changing the images will become a more convoluted process - instead of just uploading new ones and updating the DB, you will have to delete the previous (or previous set) with relative thumbnails if any before uploading the new ones; that will also mean the image change process won't be reversible unless you store the old ones in a backup directory, etc.

So, (you plan to change them often || your users will change them often) ? database : paths

cbrandolino
  • 5,873
  • 2
  • 19
  • 27
  • Please note: it's definitely faster does not mean it's very, or even perceivably, faster. – cbrandolino Dec 21 '10 at 11:54
  • even if you call a filename `along with the rest of the data`? – Your Common Sense Dec 21 '10 at 12:02
  • I'm not sure I get it. I was thinking of something like: echoing `""` and, since the $item_id var has already been assigned, I don't see how execution time or memory could be affected. Also, I am assuming that you just need to display the image. – cbrandolino Dec 21 '10 at 12:07
  • Well, considering a many-to-one relationship for the images, you'd have to JOIN the data table with the images table... I guess it's faster to just use the filesystem. – Rob Dec 21 '10 at 12:12
  • Still, I would like Col. Shrapnel to explain his comment - I don't know anything of the php internals, and he looks like he's more of an expert. – cbrandolino Dec 21 '10 at 12:38
0

Images..

  1. Imagine they are in the database.
  2. Imagine yourself caching them to improve performance.
  3. Imagine you put them in a file in order to cache them.

Ding ding ding. Images in file on harddrive.

Warren Stevens
  • 301
  • 2
  • 5
0

Storing images using a database record that holds a path to the actual image file allows you to do much more, such as store an alt text column, or any other useful information in the database as well. You will of course need to create a good content management system around that idea in order to be able to make fast and easy changes to already uploaded images.

I usually keep one table for all of my images that has the id, filename, alt_text, width, and height, then reference that table in all of my other tables that might need images attached. I also store extra stuff like resize method information, so that the next time they upload an image for that same field, you can grab and set the default values automatically for any resizing information they need to enter.

You may also want to store the image's alignment, although I usually put this in to the table that links to the image table. This allows for you to set it up so that users may easily re-use already uploaded images in other areas of the website without uploading a second copy of that image.

dqhendricks
  • 19,030
  • 11
  • 50
  • 83