Page 1 of 1

Grab file extension into string

Posted: Fri Feb 10, 2006 9:46 pm
by seodevhead
Hey guys... I am using a file upload form, but I am also using a thumbnail creation class with it to make thumbnail images. Everything works great, however I need to save the file extension (ie. .jpg .gif .png, etc) as a string in a variable.

Is there anyway to gather this from the file? I know I can view the MIME type with $_FILES[$filename]['type'], but that gives me 'image/jpg'... and all I want is 'jpg'. Any help would be GREATLY appreciated!!! Thanks.

Posted: Fri Feb 10, 2006 9:52 pm
by feyd
never rely on the type supplied to be correct (it's easily spoofed, and php doesn't check it)

Instead you should check the file itself (not the name supplied). For images, getimagesize() works quite well.

Posted: Fri Feb 10, 2006 9:57 pm
by Ambush Commander
Hmm... no need for mime_content_type, then...

Posted: Sat Feb 11, 2006 12:44 am
by josh
Isn't the mime type already in $_FILES['foo']['type'] ? Anyways to get the jpg out of that do:

Code: Select all

$mime = explode('/',$_FILES['foo']['type'];
$category = $mime[0]; // image
$type = $mime[1]; // jpg
Feyd is right though, don't rely on this information

Posted: Sat Feb 11, 2006 2:39 pm
by seodevhead
Hey guys... I used feyd's suggestion and getimagesize() works great since it returns the number corresponding to the file extension as the #2 element in the array.

You say I shouldn't rely on the mime type info because it is easily spoofed. Which method would you suggest I use to ensure the uploaded file is of the proper type? Thanks!!! :)

Posted: Sat Feb 11, 2006 2:51 pm
by feyd
the only way to ensure type is by analysing the file.
Useful Posts wrote:Some helpful information determining a file's actual type: Upload Script

Posted: Sat Feb 11, 2006 3:04 pm
by seodevhead
thanks feyd!