views:

41

answers:

3

In my application a user can create his/her photo album.

My question is how can I upload images specific to that user-id who has uploaded them? This means there should be some criteria that will indicate that these images belong to this user.

What I was thinking that I can store images with the image name of the user-id, but in that case it would be difficult to store comments along with these images.

Is there any better way?

+3  A: 

A solution that's often used is to have two data for each image :

  • The file on disk, of course
  • And a record in a database


The record in the database will generally contain at least :

  • The path to the image
  • The Id of the user who uploaded it.

This way, for each image, you have additional data -- and you can use those to add some behavior to your application.

Of course, it means you'll have a bit more work when a user uploads an image -- but it's not that bad either, and can prove useful.


When needed, too, you'll be able to add some other fields to the database's table storing those data, like, for instance :

  • Informations about users who are allowed to see each image
  • Stuff like image size, content-type, last modification time, ... To not have to re-calculate those from the files each time they are needed
Pascal MARTIN
thanks Pascal I got your point but can u provide me some sample code or some link that can give me some more idea
nectar
A: 

Do you have a database? Are users logged in? Create a table to track images, include a user ID column, and either store the entire image in the database or just the pathname to the image.

Fosco
+1  A: 

Are you not using a database?

If no:

Set up a database to store the users, then create a logical connection between the iamges and the users.

for instance

Users:

|---------------------------------------
|  UserId       Username      Password |
|---------------------------------------
|     1         Robert        3j9745t3 |
|     2          Paul         03945u30 |
|---------------------------------------

Images:

|--------------------------------------------------
|  ImageID       UserId      Desc       ImageHash |
|-------------------------------------------------|
|     1            1        Some Desc  87ytr8d23yr|
|     2            1        Some Desc  5uty4095u40|
|-------------------------------------------------|

Then store the images on your images directory like 87ytr8d23yr.png and then you can do a php query like so

$userid = 1;
$sql = "SELECT * FROM Images WHERE UserId = " . (int) $userid . " ORDER BY ImageID LIMIT 10";

if(false !== ($resource = mysql_query($sql)))
{
    while($imageRow = mysql_fetch_object($resource))
    {
       if(file_exists('imageDir/' . $imageRow->ImageHash . '.png'))
       {
            //Do whatever.
       }else
       {
           //Delete the row and inform the user that the picture has been deleted for some reason.
       }
    }
}

Also you just have to do a Comments table and add ImageId, and when a user comments on an image just add the image into the comment row so you can trace it.

RobertPitt
AS you selected this as your answer ill just inform you of some things you will need to research, Database Security (`mysql_real_escape_string` and sql injection), Table Joining, Image Security (use headers to push image type, png,jpg etc). :) happy days
RobertPitt