Use finin or pathinfo to get the mime type?

2

finfo_* and pathinfo are used to detect the mimetype of the file and not the extension.

The issue is that in the world there are several file formats and from time to time new formats appear, I can not do a test to know which function has the best support for mimetypes detection, ie it is an unfeasible test to do due to the amount of file formats, what I have doubts is about which of these functions has better support.

Question:

  • Which of these functions has the best support for detecting mime-types of files?

  • Which is better maintained by PHP?

asked by anonymous 27.05.2015 / 02:10

1 answer

2

Both are used for different purposes.

The functions of the class finfo are used to obtain information about a particular file, for example :

  • finfo_buffer : This function is used to return the textual representation of the value specified in argument string .
  • finfo_file : It has the same purpose as the finfo_buffer function, however its purpose is information from a file.
    • mime_content_type : This function is used to detect

      Mime type of a file, however it was discontinued (PHP > 4.3.0 , PHP 5 ).

  • A path_info function in turn is to return information regarding the path of the file , such as:

    • PATHINFO_DIRNAME The directory name
    • PATHINFO_BASENAME The name of the file
    • PATHINFO_EXTENSION The file extension
    • PATHINFO_FILENAME The name of the file (without the extension - PHP > = 5.2.0 )

    Notes :

    • Fileinfo has been enabled by default since PHP 5.3.0 in previous versions, Fileinfo was a pecl extension , however it is no longer maintained (last updated: 07-11-2006 ).
    • According to the introductory page, the detection capability of Mime type will depend on the following factor:

        

      The functions in this module attempt to find the content type and encoding of a file by searching for certain magic byte sequences at specific positions within the file. While this is not a bulletproof approach, the heuristic used does a pretty good job. See too:    Mime type configuration options.

    • Another way to get information about a file is to use the class SplFileInfo (from PHP 5.1.2 ).

    • Another alternative is stat function (PHP 4 , PHP 5 ).
    27.05.2015 / 06:56