Latest news
Home » PHP » Advanced method to check the mime type of file in php

Advanced method to check the mime type of file in php

Usually code is
if ($_FILES['input_field_name']['type'] == 'image/jpeg') {
//some code
}

but it is not fully validating the file types(mime).

The following code will output the correct file type and avoids the security vulnerabilities of php.

$phpfileInfo = new finfo(FILEINFO_MIME_TYPE);
$PhpfileContents = file_get_contents($_FILES['name_of_input_field']['tmp_name']);
$MimeFileType = $phpfileInfo->buffer(PhpfileContents);

Leave a Reply

x

Check Also

How to print Codeigniter upload error

if you want to display errors on upload functionality in codeigniter. Use the following function ...