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.
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).
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