tags:

views:

22

answers:

2

I need to make a script that will take files from a user and upload them to a server. I've already done this one and on my first attempt, I built in some checking to ensure that the correct mime type was being uploaded. Today, I have learned that browsers interpret mime types differently. This makes me think that maybe it isn't worth checking after all. The last thing I want is for a user to tell me that they can't upload a file because their browser doesn't handle mime types in the same way that my mine does.

What can anyone suggest. Should I forgo the mime type and check extension or is there a better way of doing this? I know that there is a CURL option for this but I think that is going to be overkill for this situation.

A: 

Take a look at FileInfo. Never rely on any data given to you by the user / browser for validation.

jasonbar
Thanks. Where are the docs? I only see User Contributed Notes there.
Jim
I agree with you about not trusting anything from the browser.
Jim
@Jim: Specifically, you'll want finfo_file: http://www.php.net/manual/en/function.finfo-file.php
jasonbar
Thanks Jason, I just found it myself. This is definitely the way to go. Thanks and cheers.
Jim
A: 

I would go with an exclusion filter. Set your supported extensions (jpeg, jpg, gif, png, txt, doc, xls, etcetera), match them and test if the images files are indeed images with getimagesize().

It's a different approach, I think it's slightly better since you don't want every filetype being uploaded.

Now that I think about it, you could implement both.

Good luck.

metrobalderas