Best practice to store user profile pic in a social network website?

Just wondering what the best practice of saving user profile pic is.

Background:
I am building a social network website a bit like a facebook clone. I have gotten to the point of making the profile page and account settings work. This question is related to how to deal with user profile picture upload.

My script currently creates a random folder in a user_files folder every time the user uploads a profile picture under “account settings”. So if you upload 3 photos, there are three folders created in the server with different(hopefully) random names.

An example of a profile picture stored in a random folder is:“./user_files/jklafopoiprewrk1241jlKDF/test.png”

My questions are:

  1. Should I link a random folder name to a specific user so that all his profile pictures are stored there?
  2. Or is it better to create a random folder every time the user uploads a profile picture?
  3. How do I clean up all these uploads later on?

Personally, I prefer uploading files (pictures, documents, etc.) directly to the database. That way it saves space on the web server AND makes clean up much simpler - when you delete it from the database, you don’t have to run another process to remove it from the web server.

HTH,

:slight_smile:

This… is marginally true, and potentially disastrously bad.

Yes, you save space on the webserver. And lose space on the database server.
You also force the database server to transmit the entire picture in it’s query response to the webserver, which then turns around and outputs it to the browser… so you double bandwidth use and slow the response time of the database server down.

Cleanup is indeed simpler. But if unlink($picture); is too difficult for you to type…why are you writing an entire separate handler for outputting the image data?

I’m also going to point out that this is a ‘social network’ which means end users are the ones pushing content into the database. Quick, allow users to upload pictures to your database and see how long before your host complains :wink:

What would you suggest as a good design to store profile picture @StarLion?

Should I delete the file and folder everytime the user changes profile picture? But I also want to keep their past uploads in a profile album like in facebook.

The only design that I can think of right now is to store the folder location in the database, and make “./user_files/jklafopoiprewrk1241jlKDF/” the folder where users store their pictures. But this solution doesn’t feel too elegant

unlink should run before rmdir()

Attempts to remove the directory named by dirname. The directory must be empty, and the relevant permissions must permit this. A E_WARNING level error will be generated on failure.

Thanks for the tip Mittineague! It’s definitely going to save me time down the road.

I changed my question a bit because I don’t want it to sound too much like a technical question, but rather a question of design philosophy.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.