0

I have written a code, where the user selects a profile picture and then the picture is stored in localhost/user/$username/photos/photo1.gif.

After that, I assigned the filename (photo1.gif) into a session variable so I can display it from all my php scripts. This is working just fine. I can display the picture in every php script by accessing this session variable.

The only problem I have is when I am trying to login from the login page: In the login page I connect to the database, retrieve email and password, check them and if they are OK I redirect the user to home.php. The problem is that the user's photo is not linked to the email so i cannot know the filename of the photo. The only thing I know for sure is the directory (because I can retrieve username from database as well).

Lets say that a user has uploaded 4 photos (photo1, photo2, photo3, photo4 - photo4 was uploaded last). It makes sense that he is currently using photo4 as my profile picture. Is there a way for me to access that folder and retrieve the filename of the picture uploaded last?

Also, as a general question, what is better, store the photos(or files) in a database or server?

idmean
  • 14,540
  • 9
  • 54
  • 83
christostsang
  • 1,701
  • 3
  • 27
  • 46

1 Answers1

1

A few options:

It would be 'better' to create a photo table and store the user_id and the photo location in that table. Storing the actual photo in the table as a blob is not generally recommended.

Alternatively, to avoid more tables, you could rename the photos as

username_photo1.jpg
username_photo2.jpg
username_photo3.jpg

And then you can retrieve the largest of them.

Finally, another option is to get the file creation date of the photos in the directory and take the most recent photo.

see Getting the filenames of all files in a folder

Community
  • 1
  • 1
ssaltman
  • 3,623
  • 1
  • 18
  • 21
  • You mean to store the url of the picture in a mysql table and then call it from there? – christostsang Jan 31 '14 at 16:57
  • yes, either store the url or the folder path. If you store the folder path, you can store it relative to root and then write a function to convert it a url - this is what I often do. Or store both url and folder path (this is what I actually do). you can add a field for "priority" and then number the photos and grab the highest priority photo - all sql. – ssaltman Jan 31 '14 at 19:28
  • Regarding tables, what is better: create a table for each section(ex. Different table for messages, different table for fotos, different table for user details, etc) or one table tha handle them all? – christostsang Jan 31 '14 at 20:33
  • if there's more than one photo per user, it should be a different table. that's a one to many relationship. the other items - you need to figure out the relationship - one to one, one to many, many to many, many to one. db architecture is well discussed on the web – ssaltman Feb 01 '14 at 01:00