views:

38

answers:

3

I have one simple question. Now I'm working on a e-commerce script and of course users will be able to upload images for each product (up to 10 images for each product). So my question is should I place all the images in the same directory (it will probably be thousands after a time) or create new ones from time to time? Will this slow down the performance or cause any other problems in future if I place them all together?

Thanks in advance

+1  A: 

Disk approach

Create an img parent directory, with a subdirectory for each product's images.

./img
./img/eggs
./img/eggs/eggs1.jpg
./img/eggs/eggs2.jpg
./img/spam
./img/spam/myspamimage.jpg
./img/cheese
...

This way you'll have all your images stored in a single tree hierarchy that makes good sense. If you're going to have a very large amount if images (say, more than 100,000) you can group the images according to creation date:

./img
./img/2010-08/eggs
./img/2010-08/eggs/eggs1.jpg
./img/2010-08/eggs/eggs2.jpg
./img/2010-09/spam
./img/2010-09/spam/myspamimage.jpg
./img/2010-09/cheese
...

This way, you will be able to move some months (probably the older ones) to an archive and keep the month subdirectory as a link to another disk.

Database approach

If you need to keep a lot of metadata on each image (e.g., username, SKU, description, copyright etc) you can store the images using arbitrary image names (probably img/img0000001.jpg, /img/img0000002.jpg, ...) and keep a database record that maps a product to its image. This is very useful for searching all the images with a certain characteristics (user, creation date, etc.) associated with them.

Adam Matan
A: 

Like adam said.

Make default directory like Images_Products

Use the mkdir function from php.net to make directories after the user uploaded an image.

Try to make the names of the directories dynamicly with some variable from the productname they added.

Example: $beer_heineken

Jordy
+1  A: 

I suggest you split them up into separate directories (maybe /year/month/ where year and month are the year and month of the time the image was uploaded, alternatively just do product_id/).

The problem is the inode structure used by most linux file systems. It needs more operations if the number of files in a directory increased.

halfdan