Use finfo or pathinfo to get the mime type?

Asked

Viewed 627 times

2

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

The point is that in the world there are several file formats and from time to time new formats appear, I have no way to make a test to know which function has better support for mimetypes detection, that is is an impracticable test to do due to the amount of file formats, what I have doubt is about which of these functions has better support.

Question:

  • Which of these functions has better support for detecting mime-types from files?

  • Which is best maintained by PHP?

  • 1

    The pathinfo is used for information on the file path, already the functions finfo_* are used to obtain information about the archive. =) So the pathinfo doesn’t catch the mime-type, but yes, the extension. 1) In this case use finfo_*. 2) The finfo_* is supported since version 5.3.0 and the pathinfo 4.0.3. Also consider the method getExtension class SplFileInfo from the 5.3.6 to get the extension. To finish, about the finfo_* see this page.

  • 1

    @qmechanik Kkkkkk I traveled, I thought pathinfo took the mimetype too

  • I formulated an answer. =)

1 answer

2


Both are used to different purposes.

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

  1. finfo_buffer: This function is used to return the representation textual of the value specified in the argument string.
  2. finfo_file: It serves the same purpose as function finfo_buffer, but it is intended to obtain information from a file.

To function path_info in turn aims to return information relating to the file path, as:

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

Notes:

  • The Fileinfo is enabled by default since PHP 5.3.0, in previous versions, the Fileinfo was a pecl extension, but is no longer maintained (last update: 07-11-2006).
  • According to the page introductory, the ability to detect Mime type will depend on the following factor:

    The functions of this module try to find the type of content and encoding of a file, looking for certain sequences of magic bytes at specific positions within the file. While this is not a bullet-proof approach, the heuristic used does a very good job. See also: Settings of Mime type.

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

  • Another alternative is the function stat (PHP 4, PHP 5).
  • 1

    Thank you, I think I confused myself because of the other answer, in that question about zip, was pure distraction my rs. Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.