Sunday, March 4, 2012

PHP file upload MIME type is unreliable

I ran into an odd issue this week where a PDF being uploaded by a user through the latest Firefox wasn't properly detected as a PDF.  The "type" showed a value of "application/x-word-xxx" instead of "application/pdf".  On my own computer, Firefox and Chrome worked fine as expected.

The file type header is defined by the browser handling the uploading.  The fix for this appears to be to use mime_content_type on the file after it's been uploaded instead of relying on $_FILES.

echo mime_content_type('php.gif') . "\n"; //image/gif
echo mime_content_type('test.php'); //text/plain

No comments:

Post a Comment