dfridrich/php-mime-type
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
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
- Dennis Fridrich
- Nick Shek
- Giso Stallenberg
- sml-joyo
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.
dfridrich/php-mime-type 适用场景与选型建议
dfridrich/php-mime-type 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 286.95k 次下载、GitHub Stars 达 17, 最近一次更新时间为 2014 年 02 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「mimetype」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dfridrich/php-mime-type 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dfridrich/php-mime-type 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dfridrich/php-mime-type 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Ankgor common support helpers, contracts, and traits required by various Ankgor packages. Validator functionality, and basic controller included out-of-the-box.
SierraTecnologia common support helpers, contracts, and traits required by various SierraTecnologia packages. Validator functionality, and basic controller included out-of-the-box.
Determine file mime type in KWCMS
Rinvex common support helpers, contracts, and traits required by various Rinvex packages. Validator functionality, and basic controller included out-of-the-box.
Alfabank REST API integration
SierraTecnologia common support helpers, contracts, and traits required by various SierraTecnologia packages. Validator functionality, and basic controller included out-of-the-box.
统计信息
- 总下载量: 286.95k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 17
- 点击次数: 14
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-02-25
