wwaz/mimetype-php 问题修复 & 功能扩展

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

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

wwaz/mimetype-php

最新稳定版本:1.1.0

Composer 安装命令:

composer require wwaz/mimetype-php

包简介

Determines mimetype of a filepath (string) or file resource

README 文档

README

Zero-config MIME type detection for PHP – automatically via file extension or content type.

PHP

Quick Start

composer require wwaz/mimetype
use wwaz\Mimetype\Mimetype;

echo Mimetype::get('document.pdf');  // → application/pdf

get() first tries detection by file extension. If that fails (missing extension or unknown type), it automatically falls back to content-type analysis – no configuration needed.

API

Mimetype::get(string $filename): string|false

Universal method – the right choice for most cases.

Mimetype::get('image.png');           // → image/png
Mimetype::get('/var/upload/avatar');  // → image/jpeg  (via content type)
Mimetype::get('archive.tar.gz');      // → application/gzip

Mimetype::fromPath(string $filename): string|false

Detection by file extension only. Throws MimetypeException if the extension is missing.

Mimetype::fromContentType(string $filename): string|false

Detection by PHP mime_content_type() only. The file must exist on the filesystem.

Examples

1 – Validate a file upload

$mime = Mimetype::get($_FILES['upload']['tmp_name']);

$allowed = ['image/jpeg', 'image/png', 'image/webp'];

if (!in_array($mime, $allowed)) {
    throw new RuntimeException('Only JPEG, PNG and WebP are allowed.');
}

2 – Set the Content-Type header

$file = '/var/www/assets/logo.svg';
header('Content-Type: ' . Mimetype::get($file));
readfile($file);

3 – Handle a missing extension gracefully

use wwaz\Mimetype\Exceptions\MimetypeException;

try {
    $mime = Mimetype::fromPath('mysterious-file');
} catch (MimetypeException $e) {
    // Fall back to content-type detection
    $mime = Mimetype::fromContentType('/path/to/mysterious-file');
}

Dependencies

Package Purpose
ralouphie/mimey Extensible MIME type database
PHP ≥ 8.0 `string

MIT © wwaz

统计信息

  • 总下载量: 38
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-02-12

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固