How do I use $File_type

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
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

How do I use $File_type

Post by andylyon87 »

Hi guys
does anyone know how to restrict the type of file on file uploads, would I need to use an pattern matcher or is it a hidden input. I am wanting to just allow image uploads (jpg, bmp, tiff, gif extensions).

help much appreciated
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

You could use $_FILES['userfile']['type'] after the file has uploaded and compare the mime type with your allowed list, however not all browsers will send this. If you want to check for images that PHP supports you could run getimagesize() on the uploaded file - then it'll tell you exactly what it is based on the file data. Another way would be to check the extension of the file (i.e. substr the last part of the filename) - but this won't be as accurate as getimage is.
User avatar
maqmus
Forum Commoner
Posts: 30
Joined: Mon Mar 08, 2004 1:10 pm

Post by maqmus »

You can't be sure before the file is uploaded, but you could use javascript to check extension and the use exif_imagetype().
Post Reply