Coder Social home page Coder Social logo

guess-mime's Introduction

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

guess-mime's People

Contributors

mzur avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.