checking file types

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
User avatar
snpo123
Forum Commoner
Posts: 77
Joined: Sat Apr 17, 2004 6:31 pm

checking file types

Post by snpo123 »

How to I restrict certian file types when uploading a file to my server (only allow jpegs, gifs, bmps, ect)?

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

Post by feyd »

the only absolute way (for images) is use [php_man]getimagesize[/php_man]() This will determine if it's a valid (appearing) image of any of the types it knows. If it doesn't know it, it'll return FALSE.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Or check the file type when you upload it - $FILES['form_field_name']['type'] - and it will give the type such as image/gif, image/jpeg etc and you just check this value.
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

yes kettle_drum, your right. But they can change the extension of say a rar file to jpeg, and upload it. But what feyd is saying is that getimagesize() will check the file and make sure it is actually an image or not.

my .02: use both suggested methods!
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

You can change the extension type, but that doesnt change the mime type - which is what is held in the type element of the array - at least i dont believe you can change it.
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

the MIME type comes from the extension of the uploaded file.
User avatar
snpo123
Forum Commoner
Posts: 77
Joined: Sat Apr 17, 2004 6:31 pm

Post by snpo123 »

I see, so I should first use $_FILES['form_name']['type'] to make sure it has an image extention, and then use getimagesize().

One question though, what do I put in the () for get image size? The path to the uploaded file, or just use $_FILES['form']['name']?

Also, what is a MIME?

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

Post by feyd »

[php_man]getimagesize[/php_man]($_FILES['field_name']['tmp_name']);
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

snpo123 wrote:I see, so I should first use $_FILES['form_name']['type'] to make sure it has an image extention, and then use getimagesize().

One question though, what do I put in the () for get image size? The path to the uploaded file, or just use $_FILES['form']['name']?

Also, what is a MIME?

thanks.
Click on the second link below: it's the manual.
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

no, to make sure it has an image extension your going to want to find the extension of the file and check it with your extensions you want. Search this forum on finding the extension for hte file, it'd been mentioned several times the last few days.
Post Reply