Page 1 of 1
[SOLVED] Getting a file extension
Posted: Wed Mar 02, 2005 4:54 am
by rubberjohn
Is there a way to get the file extension of an image, because I want different code to be used whether the file is a .gif, .jpg or a .png?
I have been looking at 'image_type_to_extension' but I dont think this is the right thing.
Cheers in advance
Posted: Wed Mar 02, 2005 5:04 am
by CoderGoblin
Personally I would use:
http://www.php.net/manual/en/function.m ... t-type.php
I don't like relying on a filename, I like to check the file contents itself.
thats the one
Posted: Wed Mar 02, 2005 5:11 am
by rubberjohn
cheers thats just what I needed
Sorry one last thing
Posted: Wed Mar 02, 2005 5:24 am
by rubberjohn
When it returns:
image/gif
how can i split the string using the ' / ' as the marker to split the first part ' image ' into a variable and the second bit 'gif' into a variable.
This really is the last bit now
Cheers
Posted: Wed Mar 02, 2005 5:29 am
by n00b Saibot
Posted: Wed Mar 02, 2005 5:32 am
by CoderGoblin
If you do not need specific variables (array ok) you could use
$mime_array=explode("/",$mime_content_type);
Then $mime_array[0] would be "image"
$mime_array[1] would be 'gif'
Other methods include the use of regular expressions, substrings etc.
Thats really it now
Posted: Wed Mar 02, 2005 6:00 am
by rubberjohn
cheers for your help thats got it working now
Posted: Wed Mar 02, 2005 9:44 am
by feyd
if I'm expecting an image, I use
getimagesize()
Posted: Wed Mar 02, 2005 9:49 am
by pickle
I agree with ~feyd. getimagesize() doesn't rely on the extension, so it can't be spoofed with someone just putting a funky extension on a file.
Posted: Wed Mar 02, 2005 9:54 am
by CoderGoblin
pickle wrote:getimagesize() doesn't rely on the extension, so it can't be spoofed with someone just putting a funky extension on a file.
mime-content-type cannot be fooled either as it checks the file contents. It has the advantage that it doesn't matter if the file is not an image. The same methodology can therefore be used for any upload.
Posted: Wed Mar 02, 2005 9:57 am
by feyd
CoderGoblin wrote:mime-content-type cannot be fooled either as it checks the file contents. It has the advantage that it doesn't matter if the file is not an image. The same methodology can therefore be used for any upload.
uh..
mime_content_type wrote: Returns the MIME content type for a file as determined by using information from the magic.mime file. Content types are returned in MIME format, like text/plain or application/octet-stream.
Posted: Wed Mar 02, 2005 10:08 am
by CoderGoblin
Stand corrected
Just checked into the code I used previously. Actually interrogated it using linux on the server to get the type, not php.