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

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
6 + 7 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.

Check It !

Tweet It

W3 Updates

Stay informed on our latest news!

Syndicate content