Php – Verifying mime type of pdf files on upload in PHP

file-uploadpdfphp

I am working on a project that requires file uploads. I want to make sure it's secure and that only PDF files are being uploaded. I am already checking file extension but i want to make sure the file really is a pdf file.

What is the best way to check the mime type in php? I am using PHP 5.2.5 and can't seem to get fileinfo or mime_content_type() working.

for fileinfo i keep getting this:

Warning: finfo_open() [function.finfo-open]: Failed to load magic database
at '(null)'. in [snipped filename]  on line 35

Best Solution

mime types are not reliable for checking type of files. A client's browser might report it wrongly.

Check for Magic Number. PDF files start with "%PDF" (25 50 44 46).