get image extension from file uplaod

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
snicolas
Forum Commoner
Posts: 97
Joined: Tue Nov 09, 2004 8:32 am

get image extension from file uplaod

Post by snicolas »

Hi,

I am uploading images using a simple file uplaod form.
How do I get the extension of this image to check if the extension is correct or not?

s
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the answer varies from strpos()/substr() to using a regular expression, or even explode()
snicolas
Forum Commoner
Posts: 97
Joined: Tue Nov 09, 2004 8:32 am

Post by snicolas »

thansk..but nmmmm not helpingmuch.,..
code example?

s
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

$_FILES['whatever']['type'] .. much more reliable than trusting the filename.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the 'type' is arbitrary, as the browser may think it's something else. Same with the extension.. which is probably more easily changed.

The "best" thing to do is know the file format specification of the files you wish to accept, and be able to analyze the files' contents
snicolas
Forum Commoner
Posts: 97
Joined: Tue Nov 09, 2004 8:32 am

Post by snicolas »

onion2k answer was helpfull..thanks mate...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

since you are wanting to upload images, I'd suggest using getimagesize()

as that will actually analyze the file for it's type, instead of just looking at the extension.
Post Reply