Search code examples
phpuploadmime-types

PHP Does fileinfo function look inside file or just checks the extension of file


We have a PHP 7.4 code were we upload video files. All MP4 are seen as MIME=video/mp4. However in rare occasions and mp4 file is detected by PHP as video/quicktime which we do not allow.

the HTTP Post says it is a video/mp4.

enter image description here

        $finfo = finfo_open(FILEINFO_MIME_TYPE);
        $mime = finfo_file($finfo, $file['file']['tmp_name']);
        finfo_close($finfo); 

Why would mime change if extension is the same? is php looking inside the file for video format?


Solution

  • The function you are using is examining the contents of the file rather than the file name to determine what the apropriate MIME type is.

    From the Fileinfo extension documentation where the finfo_* family of functions comes from:

    The functions in this module try to guess the content type and encoding of a file by looking for certain magic byte sequences at specific positions within the file. While this is not a bullet proof approach the heuristics used do a very good job.

    The rare instances you are encountering are likely the result of someone saving or renaming a QuickTime file with the wrong extension.