Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most efficient way to name a user avatar?

Tags:

php

I want to let my users upload an avatar on their profile. My first idea was to name the avatar file like this: [user_id].jpg. So even if a user updates its avatar, it keeps the same name.

The problem with that is that if I use caching on the server (or even if it's used on the client) the new avatar won't show up.

My new solution is to name the file like this:

[user_id]_[random_number].jpg

and store the random number in the Users table. How would you generate this number in the most efficient way? Or maybe there is a better solution?

like image 754
Max Avatar asked Nov 23 '10 11:11

Max


2 Answers

You should be able to invalidate the cache when the user uploads a new avatar.

If this is not possible you could just store it as [uid]_[YYYYMMDDhhmmss].jpg or something. No need to generate anything random...

like image 91
Christian Wattengård Avatar answered Oct 13 '22 01:10

Christian Wattengård


I would do something like:

$avatarName = $userId . uniqid();
// add extension if needed, store it

It will be fast and do what you want. uniqid()

EDIT

As suggested by other users, you should drop the userId from the image name. Having a public userId may lead to problems in the future. Also, uniqid() alone should work.

$avatarName = uniqid();
// add extension if needed, store it
like image 20
acm Avatar answered Oct 13 '22 02:10

acm