dfridrich/php-mime-type 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dfridrich/php-mime-type

最新稳定版本:v3.0.1

Composer 安装命令:

composer require dfridrich/php-mime-type

包简介

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

README 文档

README

Join the chat at https://gitter.im/dfridrich/PhpMimeType Build Status Latest Stable Version Total Downloads Monthly Downloads License

"Buy Me A Coffee"

PhpMimeType

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

Install

composer require dfridrich/php-mime-type

Usage

Basic usage

<?php

// 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 application/octet-stream

// 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>';
}

// Guess FontAwesome icon
echo \Defr\PhpMimeType\MimeType::getFontAwesomeIcon('test.pdf'); // fa fa-file-pdf-o
// ...with fixed width icon
echo \Defr\PhpMimeType\MimeType::getFontAwesomeIcon('test.pdf', true); // fa fa-file-pdf-o fa-fw

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.

<?php

// 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 (and own file name)
$response = \Defr\PhpMimeType\FileAsResponse::get(__FILE__, null, "my-own-file-name.txt");
$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

Credits

Contributors

Thanks to...

  • freepik.com - it provided photo in logo
  • svogal - this guy inspired me to create this library
  • colemanw - his gist inspired me to add FontAwesome support

License

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

统计信息

  • 总下载量: 265.87k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 16
  • 点击次数: 0
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 16
  • Watchers: 2
  • Forks: 7
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-02-25