Giter VIP home page Giter VIP logo

phpmimetype's Introduction

PhpMimeType

Build Status Latest Stable Version Total Downloads License

PhpMimeType

Simple PHP class for guessing file mime type based on file extension with ability to use in Symfony project.

Install

Via Composer

$ composer require dfridrich/php-mime-type

Usage

Basic usage

// from string, can be used on non-existing files
echo \Defr\PhpMimeType\MimeType::get('index.php'); // outputs text/html

// from SplFileInfo
echo \Defr\PhpMimeType\MimeType::get(new \SplFileInfo('Video.avi')); // outputs text/html

// from SplFileObject
echo \Defr\PhpMimeType\MimeType::get(new \SplFileObject('Image.JPEG')); // outputs image/jpeg

// from string
echo \Defr\PhpMimeType\MimeType::get('someStrange.extension'); // outputs application/octet-stream

// Multiple files
$files = ['index.php', new \SplFileInfo('Video.avi'), new \SplFileObject('example.php')];
/** @var \Defr\PhpMimeType\MimeTypeInfo[] $mimeTypes */
$mimeTypes = \Defr\PhpMimeType\MimeType::multiple($files);

foreach ($mimeTypes as $mimeType) {
    echo sprintf('File "%s" is mime type "%s"', $mimeType->getFileName(), $mimeType->getMimeType()).'<br>';
}

Symfony response

If you want to use Symfony response feature, install HTTP Foundation package too.

$ composer require symfony/http-foundation

Just pass the file name or SPL object to response method and you will get Symfony\Component\HttpFoundation\Response object. Disposition is attachment by default, you can chage it to inline or use Symfony ResponseHeaderBag's constants DISPOSITION_ATTACHMENT or DISPOSITION_INLINE.

// Return response to download this file as attachment (default)
$response = \Defr\PhpMimeType\MimeType::response(__FILE__);
$response->send();

// Return response to download this file inline
$response = \Defr\PhpMimeType\MimeType::response(__FILE__, \Symfony\Component\HttpFoundation\ResponseHeaderBag::DISPOSITION_INLINE);
$response->send();

// You can use FileAsResponse object too
$response = \Defr\PhpMimeType\FileAsResponse::get(__FILE__);
$response->send();

// Or directly send it to browser
$response = \Defr\PhpMimeType\FileAsResponse::send(__FILE__);

More examples and documentation

See more examples.

API documentation can be found here.

Testing

$ composer test

Contributing

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don't break it in a future version unintentionally.
  • Commit just the modifications.
  • Ensure your code is nicely formatted in the PSR-2 style and that all tests pass.
  • Send the pull request.
  • Check that the Travis CI build passed. If not, rinse and repeat.

Credits

Photo by freepik.com

License

The MIT License (MIT). Please see License File for more information.

phpmimetype's People

Contributors

dfridrich avatar gisostallenberg 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.