View Full Version : Testing for file extension
dwh
Wed 15th Aug '01, 5:15am
I need to test for a file extension of a variable and do one thing if it's a gif and one thing if it's a jpeg. I don't even know how to look this up in php manual. I did a search on "contains", what a waste of time! Maybe one of you good folks knows this?
JamesUS
Wed 15th Aug '01, 7:55am
Take a look at the strxxx functions - if you find an alphabetical function listing you should be able to see them all. I can't remember which one will suit you, I think it is strstr() but I might be wrong.
dwh
Wed 15th Aug '01, 2:58pm
Thanks. Someone came up w/ some great code for me on another forum.
<?php
$image_location = img.gif;
$image_type = array( 1 => 'GIF',2=> 'JPG',3=>'PNG',4=>'SWF' );
if($img = @GetImageSize ("$image_location"))
{
echo "<b>Image exsists</b>";
$type = $image_type[$img[2]];
print "type : $type";
}
else
{
echo "Image does not exsist";
}
?>
Just to give him credit here's his sig:
==================================
D.K.Jariwala
~ Simple Thought, Simple Act ~
URL ::: JDK HomePage
E-Mail ::: boyzone_2001@yahoo.com
==================================
BDJ
Wed 15th Aug '01, 5:10pm
Hmm...not sure, but maybe something like this..
<?php
// I'm gonna assume the file is called $img..so just fill in appropriately.
if ( preg_match('/(.*)\.gif/',$img) ) {
// file is gif.
// put in your gif execution code here.
}
elseif ( (preg_match('/(.*)\.jpg/',$img) ) or (preg_match('/(.*)\.jpg/',$img) ) ) {
// file is jpeg.
// that second (preg_match statement is optional..you can remove anything after the first if you want..
// execute jpeg code.
}
else {
// File is incorrect type. Give user and error message.
}
I'm not sure if that'd work or not..but that's what popped into my head.
vBulletin® v3.8.0 Beta 4, Copyright ©2000-2008, Jelsoft Enterprises Ltd.