Best way to get mime type file in php

What is the best and safe msit way to get mime file type? I already found this:

http://de3.php.net/manual/de/ref.fileinfo.php

But fileinfo () seems to be easy to manipulate. Is there a better way to get the mime file type? I need this for image files, by the way.

+3
source share
3 answers

Here is another way

<?php
$info = getimagesize("image.gif");
$type = $info['mime'];
?>

But it would help if you could say exactly what you mean by "security" and what kind of manipulations you mean.

+3
source

getimagesize () returns a mime type also http://php.net/manual/en/function.getimagesize.php

<?php
//png image called gif 
$image = 'temp.gif';
$size = getimagesize($image);

if (isset($size['mime']) && $size[0]>0 && $size[1]>0) {
    echo"Content-type: {$size['mime']}";
    //outputs Content-type: image/png
} else {
     echo"not an image";
}

?>
+2
source

, - . , PECL , , , , , . . , -

http://www.garykessler.net/library/file_sigs.html

- for file signatures (or magic bytes or what they call now). You know what types of files you are going to accept, so just scan the incoming file for a set of signatures.

Another option is to let PHP run the unix shell and use the command filewith the appropriate flags to check your file and get the true mime type based on the actual contents of the file.

+1
source

All Articles