Determining file type using PHP. What is a magic database?

I am trying to determine if some files are actually images (using PHP). I was advised to use finfo and I am trying to understand how this works.

What I am not getting is a database of magic numbers and how does it work? I'm a little puzzled - does each file have a specific β€œmagic number” that you are comparing with this database?

Also, I have this on my debian squeeze - but will it also be available on the WIN platform? or will you need to attach this database with the application?

<?php $finfo = new finfo(FILEINFO_MIME, "/usr/share/misc/magic.mgc"); if (!$finfo) { echo "Opening fileinfo database failed"; exit(); } /* get mime-type for a specific file */ $filename = "/usr/local/something.txt"; echo $finfo->file($filename); ?> 
+6
source share
3 answers

Most file formats have a header that helps determine which file it is. For example, GIFs always start with GIF87

The magic number database is a list of all headers and allows finfo() identify files.

On Windows, this database is not installed by default. You will need to bring it for windows. In fact, you should use the same database no matter where you are deploying in order to improve cross-platform compatibility. Imagine if you deployed to an old system that does not know about the types of files that the dev platform understands.

+6
source

Will an alternative solution see if exif_imagetype false ?

+1
source

On my Ubuntu, it is in /usr/share/file/magic.mime . I do not know about Windows. And yes, usually there is a special prefix for different file formats (even if there is no extension, you can recognize GIF, for example, because it always starts with the string "GIF").

+1
source

Source: https://habr.com/ru/post/888269/


All Articles