views:

61

answers:

3

im developing a forum and users will be able to upload pictures.

i wonder how i should organize the folders for this?

should they be sorted by threads or users? but how will they then be organized within these folders?

and how could i couple some post's pictures to the database entry? eg, if the post got 5 images uploaded, should i have a one-to-many (one thread - many pictures) relationship where the pic entries contain the name of the pictures?

i've never done this before.

would be great if someone could give me some piece of advice.

+1  A: 

You're asking about the backend storage sorting of the image, on a filesystem, right? I would organize it like this: imagefolder/user.id/image.jpg

Because it's much more simpler than doing something like year/mont/day/thread..

--

Just seen the update.

For your database part, I would say you don't really need to save it in a separate table. If you save a thread's posts in a row, you can save html in there and provide some window where a user can check which images he want to insert / upload. this writes the according html tag relative to the saved position. So basically just an extension of an editor like stackoverflow has here.

Saving images / paths in relation to posts/threads is only necessary if you need to store additional data with it, e.g. creation time, comments, ratings etc.

pduersteler
exactly...sound like a good advice...then on the database i couple the thread message with one-to-many relationship right?
weng
you can couple it, if you need it. or you can go the simple way and just save your post html where image tags are provided. so you don't need to have an additional table.But, like i wrote, this only works if you don't need additional data or sth.
pduersteler
+1  A: 

Suggestions:

  • don't mix filenames with your thread structure
  • filenames should be unique - probably the id of the db entry related
  • use a folder system in order to limit the number of potential files in one folder

I used something like 1.jpg, 2.jpg for image names. Each folder has 1000 images and the name of the folder will be 00000001 for first 1000 images ( the thousand figure padded with 0 until 8 figures). This will allow you to store 100.000.000.000 images and fast access.

The relationship between images and threads should be a MN table with 2 indexes for threadID and imageID.

Elzo Valugi
could i ask why a many-to-many relationship? cause their will be one thread/post with one or many pictures. so that will be perfect for one-to-many relationship?
weng
no particular reason. Do whatever you feel like to, but I would relate the images when uploaded not to a particular thread but to the user that owns the picture. This way the same picture can be reused in other thread without the need of re-uploading. This is a design issue and if you want images related to threads, is nothing wrong with that and you can ignore my MN suggestion.
Elzo Valugi
i will in my picture table have "id, thread_id/post_id and user_id" so in this way its still user ortiented
weng
+2  A: 

Do you expect to handle/copy/transfer/delete a thread's worth of images in one go? If so, it may make sense to create a folder for each thread. Otherwise there would be no particular benefit. In fact it could be a drawback... for example if you can move posts from one thread to another or merge threads, you'd then have to write extra code to move the image files.

eg, if the post got 5 images uploaded, should i have a one-to-many (one thread - many pictures) relationship

Yes, or maybe one post many pictures?

where the pic entries contain the name of the pictures?

If you remember the filenames of the uploaded files, it should only be for display purposes in the thread. Don't use a user-submitted filename as a filename on your server's filesystem. This risks clashes when two users upload files with the same name, and anyway ‘sanitising’ user names for security is difficult to get right.

Better, give each database picture row a primary key, and use that key to name the file, for example 5272.jpeg. You should also really be serving the uploaded files from a different hostname, otherwise there are all sorts of cross-site-scripting risks.

See this question for some background. Dealing with user-uploaded files securely is not at all easy, and almost every file-upload tutorial out there is full of security holes.

bobince
yes one post many pics were what i really meant. how do i do with thumb nail images? i want to show thumb nails in a thread, then when the user point at it with the mouse the full size will be shown. if the full size image is named 123.jpg, what will the thumb nail's name be and how will i structure the database for it?
weng
I'd call it `123.thumb.jpeg` or something similar, so you can use the same primary key to access it easily.
bobince