[SOLVED] Getting a file extension

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
rubberjohn
Forum Contributor
Posts: 193
Joined: Fri Feb 25, 2005 4:03 am

[SOLVED] Getting a file extension

Post 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
Last edited by rubberjohn on Wed Mar 02, 2005 5:59 am, edited 1 time in total.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
rubberjohn
Forum Contributor
Posts: 193
Joined: Fri Feb 25, 2005 4:03 am

thats the one

Post by rubberjohn »

cheers thats just what I needed
rubberjohn
Forum Contributor
Posts: 193
Joined: Fri Feb 25, 2005 4:03 am

Sorry one last thing

Post 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
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

use explode() function.
Last edited by n00b Saibot on Wed Mar 02, 2005 10:14 am, edited 1 time in total.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
rubberjohn
Forum Contributor
Posts: 193
Joined: Fri Feb 25, 2005 4:03 am

Thats really it now

Post by rubberjohn »

cheers for your help thats got it working now
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

if I'm expecting an image, I use getimagesize()
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Stand corrected :oops:

Just checked into the code I used previously. Actually interrogated it using linux on the server to get the type, not php.
Post Reply