2

I have a lot of records in the database, and each record will have an image, I'm pretty confused about how to store the images.

I want the access route to be something like /img/record-id.jpg (i.e. /img/15178.jpg).

Alright, storing all images inside /img/ isn't a good thing, because there will be many.

In this question it is suggested to reverse the name of the image, so the example above would be stored under /img/78/51/15178.jpg. The suggestion won't give further info (and for me it's not obvious) about other scenarios. What will happen (this is asked in the last comment for the answer) if the id is a low number like 5, 15, 128, 1517?

Leaving that aside, let's remember I want the path to be /img/15178.jpg. I'd redirect the request using Apache, but for that I'd have to type at least 3 or more rules for different id numbers:

  • ^/img/(\d)(\.jpg)$ /img/$1$2
  • ^/img/(\d\d)(\.jpg)$ /img/$1/$1$2
  • ^/img/(\d\d)(\d\d)(\.jpg)$ /img/$1/$2/$3
  • And so on?

This doesn't seem to be a nice solution, although it would work just fine.

I could think of other option which is: take the MD5 of the image, store it in its respective record, redirect the request to a PHP script and let it take care of the output.

The script will look the MD5 for the id in the database, build the actual route out of the hash and output the image. This solution is neater, but it involves database and PHP to output an image, sounds like a little too much.

I really don't know what to do here. Mind giving me some advice?

Community
  • 1
  • 1
Parziphal
  • 6,222
  • 4
  • 34
  • 36
  • 1
    "Alright, storing all images inside /img/ isn't a good thing, because there will be many." - how many do you expect? why do you think this is a problem? – Pavel May 17 '12 at 14:35
  • +1 for Pavel. `redirect the request to a PHP script and let it take care of the output` I think this is a bad idea. Let your web server serve images directly, it will handle a lot of things for you like resource caching. About the MD5, you could store the image in /some/path/img/123456789.jpg and link directly to this image `` (123456789 would be the md5 string). Also, you could combine both technique: generate and md5, make sure it is always the same length, split it into n parts, and use the technique you provide (/img/78/51/15178.jpg) – pomeh May 17 '12 at 14:36
  • @Pavel I expect thousands, 5000+. I don't like the idea of having 1000+ files in a single directory. Or this isn't a problem? – Parziphal May 17 '12 at 14:47
  • @pomeh Yeah, but the thing is, the name of the images is the id of the record it belongs to, so it will go from 1 to 5000+. – Parziphal May 17 '12 at 14:50
  • @renocor and so, what is the problem ? – pomeh May 17 '12 at 14:51
  • @renocor why does it matter if there are 1000+ files arbitrarily in a single directory or 1000+ arbitrary directories with a single file? – Explosion Pills May 17 '12 at 14:51
  • What's file system you are using? Perhaps this one will be of interest: http://serverfault.com/questions/129953/maximum-number-of-files-in-one-ext3-directory-while-still-getting-acceptable-per – Pavel May 17 '12 at 14:57
  • I was going to point out http://stackoverflow.com/questions/466521/how-many-files-in-a-directory-is-too-many saying that there isn't a technical reason for not having lots of files in a directory. It sure does make manual backups a pain though! – creuzerm May 17 '12 at 15:25

1 Answers1

2

You already have written the perfect answer ! Professionals use it exactly like you (or the guy in the linked question) says: Building a deep directory structure that fits your needs. I have done this with 16 million pictures, and it worked perfectly.

I did it like this:

/firstCharacter/secondCharacter/...

Files with short names, like 5.jpg, will be in /5/5.jpg

EDIT: to keep the performance on top, i'm totally against any further php actions, like salt, md5, etc. Keep it straight and simple.

Sliq
  • 15,937
  • 27
  • 110
  • 143
  • You could always just put the 5.jpg image in the /images/5/ directory. – creuzerm May 17 '12 at 15:06
  • Can you please explain the method further, or share a link where this method is explained? I still don't understand, how would you redirect the request through htaccess? 1458.jpg would be in /1/4/5/1458.jpg? – Parziphal May 17 '12 at 18:14
  • @renocor ;) exactly ! no redirect here ! simple put a / between each of the filename's characters. – Sliq May 17 '12 at 18:23