yousefkadah/laravel-magika
Composer 安装命令:
composer require yousefkadah/laravel-magika
包简介
Laravel package for AI-powered file type validation using Google's Magika model
README 文档
README
AI-powered file type validation for Laravel using Google's Magika — a deep learning model (~1 MB) that achieves ~99% accuracy across 200+ content types.
Unlike traditional MIME-type checking (which relies on file extensions or magic bytes), Magika uses a trained neural network to accurately identify file content, making it significantly harder to bypass with spoofed extensions or headers.
Requirements
- PHP 8.1+
- Laravel 10, 11, or 12
- Magika CLI installed on your system
Installing the Magika CLI
Choose one:
# macOS / Linux (Homebrew) brew install magika # Python (pipx) pipx install magika # Rust (Cargo) cargo install --locked magika-cli
Installation
composer require yousefkadah/laravel-magika
Publish the configuration (optional):
php artisan vendor:publish --tag=magika-config
Configuration
// config/magika.php return [ 'binary_path' => env('MAGIKA_BINARY_PATH', null), // null = use system PATH 'prediction_mode' => env('MAGIKA_PREDICTION_MODE', 'high-confidence'), 'timeout' => env('MAGIKA_TIMEOUT', 30), ];
Usage
Detecting File Types
use YousefKadah\LaravelMagika\Facades\Magika; // Detect a file by path $result = Magika::detect('/path/to/file.pdf'); $result->label; // "pdf" $result->mimeType; // "application/pdf" $result->group; // "document" $result->description; // "PDF document" $result->score; // 0.99 $result->isText; // false $result->extensions; // ["pdf"] // Detect from raw content $result = Magika::detectContent('<?php echo "hello";'); $result->label; // "php" // Detect an uploaded file $result = Magika::detectUploadedFile($request->file('document')); // Batch detection $results = Magika::detectMany(['/path/to/file1.pdf', '/path/to/file2.jpg']);
Validation Rules
By Magika Label
Validate that an uploaded file matches specific Magika content type labels:
// Using string rule (in controller or form request) $request->validate([ 'document' => 'required|file|magika:pdf,docx,xlsx', ]); // Using invokable rule use YousefKadah\LaravelMagika\Rules\MagikaFileType; $request->validate([ 'document' => ['required', 'file', new MagikaFileType(['pdf', 'docx', 'xlsx'])], ]);
By MIME Type
// Using string rule $request->validate([ 'image' => 'required|file|magika_mime:image/png,image/jpeg', ]); // Using invokable rule use YousefKadah\LaravelMagika\Rules\MagikaMimeType; $request->validate([ 'image' => ['required', 'file', new MagikaMimeType(['image/png', 'image/jpeg'])], ]);
By Group
// Using string rule $request->validate([ 'upload' => 'required|file|magika_group:document,image', ]); // Using invokable rule use YousefKadah\LaravelMagika\Rules\MagikaGroup; $request->validate([ 'upload' => ['required', 'file', new MagikaGroup(['document', 'image'])], ]);
Checking Installation
use YousefKadah\LaravelMagika\Facades\Magika; Magika::isInstalled(); // true/false Magika::version(); // "magika 0.1.0-rc.3 (model standard_v3_3)"
MagikaResult API
| Property | Type | Description |
|---|---|---|
path |
string | File path that was analyzed |
label |
string | Content type label (e.g. pdf, python) |
mimeType |
string | MIME type (e.g. application/pdf) |
group |
string | Group category (e.g. document, code) |
description |
string | Human-readable description |
score |
float | Confidence score (0.0 - 1.0) |
isText |
bool | Whether the content is text-based |
extensions |
array | Possible file extensions |
status |
string | Detection status (ok or error) |
| Method | Returns | Description |
|---|---|---|
isOk() |
bool | Whether detection succeeded |
matchesLabel(string|array) |
bool | Check against label(s) |
matchesMimeType(string|array) |
bool | Check against MIME type(s) |
matchesGroup(string|array) |
bool | Check against group(s) |
Common Magika Labels
| Label | MIME Type | Group |
|---|---|---|
pdf |
application/pdf |
document |
png |
image/png |
image |
jpeg |
image/jpeg |
image |
gif |
image/gif |
image |
docx |
application/vnd.openxmlformats-officedocument.wordprocessingml.document |
document |
xlsx |
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet |
document |
zip |
application/zip |
archive |
python |
text/x-python |
code |
javascript |
text/javascript |
code |
html |
text/html |
code |
csv |
text/csv |
code |
json |
application/json |
code |
xml |
text/xml |
code |
mp3 |
audio/mpeg |
audio |
mp4 |
video/mp4 |
video |
For the full list of 200+ supported types, see the Magika content types documentation.
Why Use Magika Over Standard Validation?
Laravel's built-in mimes and mimetypes rules rely on file extensions and PHP's finfo (which uses libmagic). These can be easily tricked:
- Renaming
malware.exetomalware.pdfbypasses extension checks - Crafted files can fool magic-byte detection
Magika uses a deep learning model trained on ~100M files, analyzing actual file content patterns — making it far more robust against evasion.
License
MIT License. See LICENSE for details.
yousefkadah/laravel-magika 适用场景与选型建议
yousefkadah/laravel-magika 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 3, 最近一次更新时间为 2026 年 04 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「validation」 「laravel」 「ai」 「mime-type」 「magika」 「file-detection」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yousefkadah/laravel-magika 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yousefkadah/laravel-magika 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yousefkadah/laravel-magika 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Mime-Types
PHP package for converting file extensions to MIME types and vice versa.
Package that provides mime type icons
Adds request-parameter validation to the SLIM 3.x PHP framework
A jQuery augmented PHP library for creating and validating HTML forms
A Laravel validator for delimiter-separated list of emails.
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 34
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-04-18