waxical
waxical

Reputation: 3896

Getting mime type from uploading files - inconsistent

I've got a script, largely based on an example uploading PHP file from jQuery Uploader. It gets file type with the following code (it gets this $_FILES component)...

$fileType = (isset($_SERVER['HTTP_X_FILE_TYPE']) ? $_SERVER['HTTP_X_FILE_TYPE'] : $upload['type']);

Note; $upload['type'] comes from the $_FILES['files']['type'].

Now, this is fine - except for the fact that some files seem to have no fileType information from this. I can get more accurate responses from using file info and mimetype functions in PHP - but they don't work on $_FILES objects and I'm trying to do this check before I transfer the file to s3 so I don't really want to load it locally.

Can anyone advise if there's something I can to get more accurately report type from $_FILES or is it going to have to load locally in order to run these alternative PHP functions?

Upvotes: 0

Views: 582

Answers (1)

DaveRandom
DaveRandom

Reputation: 88647

finfo is the only way to do this. You cannot rely on information the client sends you, it is far too easy to fake from the client side.

There is no reason that it won't work with $_FILES, you would simply pass $_FILES['files']['tmp_name'] as the file path - this is still a valid file path, and you don't need to call move_uploaded_file() to access the data. Leaving the file in the temp location also has the advantage that it will be destroyed when the script is finished if you haven't done anything with it.

Upvotes: 1

Related Questions