Page 1 of 1

checking file types

Posted: Mon Jun 21, 2004 8:00 pm
by snpo123
How to I restrict certian file types when uploading a file to my server (only allow jpegs, gifs, bmps, ect)?

Thanks.

Posted: Mon Jun 21, 2004 8:05 pm
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.

Posted: Mon Jun 21, 2004 11:09 pm
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.

Posted: Tue Jun 22, 2004 12:09 am
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!

Posted: Tue Jun 22, 2004 12:11 am
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.

Posted: Tue Jun 22, 2004 12:16 am
by Illusionist
the MIME type comes from the extension of the uploaded file.

Posted: Tue Jun 22, 2004 11:27 am
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.

Posted: Tue Jun 22, 2004 12:47 pm
by feyd
[php_man]getimagesize[/php_man]($_FILES['field_name']['tmp_name']);

Posted: Tue Jun 22, 2004 1:21 pm
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.

Posted: Tue Jun 22, 2004 4:01 pm
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.