views:

67

answers:

2

How do I store a photo on the server.

I store them in a directory - "D:\zjm_code\basic_project\pinax\media\default\pinax\images\upload" but this now a lot of images.

Is there another simple way?

Thanks

+1  A: 

There are two common options.

1) Store them on the file system on the server, preferably not all in one directory - but split logically.

2) Store the images in a database, if you are using MySql you would do this using the "blob" type.

Sohnee
+1. Storing the images in database is not common practice for web apps, however it is much convenient :) Pro: there is no need to synchronize db and file system. Contra: need to serve images from database with separate handler/view/script.
Yaroslav
A: 

When you store all the images in one directory it could quickly become unmanaged due to huge number of files. You can put your images in subdirectories based on md5 or sha1 hash function. Usually 2 levels of subdirs is enough.

In this case your directory images/upload will contain subdirs with the names 00, 01, 02 .. FF (256 names total) and each of these 256 directories in turn will contain directories with names 00 through FF giving you in total 256*256 directories on level 2.

For each uploaded file you calculate hash function (not necessary based on it's content, but on some unique data) and take [0:2] as a name of first directory and [2:4] as a name of second level directory.

This would be helpful in creating directories: http://stackoverflow.com/questions/600268/mkdir-p-functionality-in-python

Yaroslav