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?
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...
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With