Skip to content
This repository has been archived by the owner on Apr 21, 2022. It is now read-only.
/ guess-mime Public archive

PHP package to guess the MIME type from the file extension (Linux only).

License

Notifications You must be signed in to change notification settings

mzur/guess-mime

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Guess MIME

Tests

Abandoned: Use league/mime-type-detection instead.

Guess the MIME type from the file extension (Linux only). This can be handy if the file does not exist or cannot be accessed.

Warning: This package should not be used if the file actually exists and can be accessed (e.g. to check user-uploaded files). Use finfo_file for that.

Installation

composer require mzur/guess-mime

By default this package requires the /etc/mime.types file to be present. It can be installed e.g. with the mime-support package (Ubuntu) or the mailcap package (Alpine). A different file can be configured, too (see below).

Usage

use Mzur\GuessMIME\GuessMIME;

$gm = new GuessMIME;
$mime = $gm->guess('image.jpg');
var_dump($mime); // image/jpeg

If a MIME type cannot be guessed, application/octet-stream is returned. You can also limit the available MIME types, use a different MIME type database file (default: /etc/mime.types) and use a strict check that returns null if the MIME type cannot be guessed:

use Mzur\GuessMIME\GuessMIME;

// Limit detection to image/jpeg and use a different database file.
$gm = new GuessMIME(['image/jpeg'], '/home/user/.mime.types');

// Default MIME type.
$mime = $gm->guess('image.png');
var_dump($mime); // application/octet-stream

// Use strict check.
$mime = $gm->guess('image.png', true);
var_dump($mime); // null

About

PHP package to guess the MIME type from the file extension (Linux only).

Topics

Resources

License

Stars

Watchers

Forks

Languages