webCOMAND

io_comand_util\mime_type

Determine the MIME Type and related information from a file, extension and/or data.

MIME Types are determined using a technique that analyzes the beginning of a file to discover a known sequence of bytes or pattern often called Magic Numbers.

A MIME Type is a two-part identifier separated by a slash that looks like:

  • application/pdf
  • application/zip
  • audio/mpeg
  • image/jpeg
  • image/png
  • video/mp4
  • video/mpeg

When a specific MIME Type can not be determined, the generic text/plain or application/octet-stream MIME Types (or txt and bin extensions) are typically returned as a fallback to indicate an unrecognized plaintext or binary file.

Example

use \io_comand_util\mime_type;

$ext = 'pdf';
$filename = '/docs/file.' . $ext;
$data = file_get_contents($filename);

$mime_type = mime_type::get_mime_type_from_ext($ext);
echo("MIME Type from extension $ext is $mime_type\n");

$mime_type = mime_type::get_mime_type_from_file($filename);
echo("MIME Type from file $filename is $mime_type\n");

$mime_type = mime_type::get_mime_type_from_data($data);
echo("MIME Type from file data is $mime_type\n");

$ext = mime_type::get_extension_from_mime_type($mime_type);
echo("Extension from MIME Type is $ext\n");