Guess file extension

Hi ALL!

I’m not sure if that’s possible to do, but I’m sure some of you will know the answer.

I store the users’ avatars in a folder ‘avatars’ and they are all renamed to : user_name.FILE_EXTENSION —> FILE_EXTENSION can be .jpg, .gif, .png, .bmp

Now I’d like to display those avatars but I DON’T WANT to read the folder and check file extensions! The php script ‘knows’ the user_name but I can’t display the avatar like


echo "<img src='avatars/".$user_name.".jpg' />";

because they’re not all .jpg’s
Is it possible to select the right extension without reading the folder for it?
Thank you advanced!

The OP could go through and change the avatar formats quite easily but if any of the avatars are animations they need to stay as a gif.
I doubt transparency comes into it so he could change everything that is not a gif to a jpg which would be one less format to search for. Then automaticaly convert any new avatars that are not gif on upload to jpg.

Without actually reading the directory all you can do is take a stab in the dark (and since you have 4 possible extensions there is a 25% chance you’ll get it right).
What you could do is convert all avatars to jpg (or png or gif if you like) upon upload, then the extension is always the same.
Oh, and it’s not a very good idea to use BMP images on the web; only IE displays those.

I suppose your could check for the username.jpg first and if that does not exist try the username.gif and if that does not exist then try username.png

As ScallioXTX says you should not be using .bmp files.

I’d convert the avatars to a standard format. Easy as pie with ImageMagick.