Detecting a mime type fails in php

前端 未结 2 613
鱼传尺愫
鱼传尺愫 2021-01-19 23:59

I have the following PHP code that shows the mime type of an uploaded file.



        
相关标签:
2条回答
  • 2021-01-20 00:57

    From PHP Mimetype introduction:

    This extension has been deprecated as the PECL extension Fileinfo provides the same functionality (and more) in a much cleaner way.

    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.

    This extension is derived from Apache mod_mime_magic, which is itself based on the file command maintained by Ian F. Darwin. See the source code for further historic and copyright information.

    From PHP Fileinfo introduction:

    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.

    Here's a question with some answers on the same subject: Detecting MIME type in PHP.

    0 讨论(0)
  • 2021-01-20 01:01

    I don't have a Unix box here to inspect a real "magic" file (the signatures database used to guess mime types) but a quick Google search revealed this:

    # $File: fortran,v 1.6 2009/09/19 16:28:09 christos Exp $
    # FORTRAN source
    0       regex/100       \^[Cc][\ \t]    FORTRAN program
    !:mime  text/x-fortran
    

    Apparently, it scans the start of the file looking for lines that begin with a single C letter plus spaces, which seem to be a Fortran style comment. Thus the false positive:

    somecolumn;
    C F;
    
    0 讨论(0)
提交回复
热议问题