Avatar validation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Avatar validation

Post by Joe »

I was wondering if anyone knew any method to check image validation. For example if someone enters an avatar url the script checks to see if it ends in .jpg, .gif or .bmp

I thought ereg() but im not too sure on the workaround of that!

Regards


Joe 8)
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

untested and off the top of my head:

Code: Select all

$wildcard = preg_match("/(\.gif|\.jpg|\.bmp|\.png)/i",$filename,$matches);
print_r($matches[1]);
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

that will work

.ai, .jpeg can/should be added
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

this may be slightly better:

Code: Select all

function validFilename($filename)
{
  $exts = array('jpe?g','gif','bmp','png','ai');
  $exts = implode('|',$exts);

  return (bool)preg_match("/\.($exts)$/i",$filename);
}
I can write up something to check the actual image data of each of those, if you want it as well.
Post Reply