what are the ways to check image mime types in php?
Top Questions
There are a few inbuilt options you can use however, for example getimagesize() can return the mimetype, as does some of the new fileinfo functions. The mime type in getimagesize is stored in 'mime', and can be accessed as shown below.
<?php
$parts = getimagesize($filename);
echo $parts['mime'];
?>or
<?php
$parts = getimagesize($filename);
$allowedMimes = array('image/jpg', 'image/png', 'image/gif');
if(in_array($parts['mime'], $allowedMimes))
echo 'Valid Mimetype!';
?>other way
<?php
$filename = 'maliciousScript.jpg.php'; // filename to check
$parts = explode('.', $filename); // common (but wrong) method used
echo $parts[1];
echo strrchr($filename, '.'); // correct method
?><?php
echo system('file -ib '. $filename);
?>
Post new comment