scafera/file
Composer 安装命令:
composer require scafera/file
包简介
File handling for the Scafera framework
README 文档
README
File handling for the Scafera framework. Upload, validate, store, and serve files — all behind Scafera-owned types.
Internally adopts symfony/http-foundation Upload, symfony/mime, and symfony/filesystem. Userland code never imports Symfony file types — boundary enforcement blocks it at compile time.
Provides: File handling for Scafera — upload (
UploadExtractor→UploadedFile), validate (UploadValidator+UploadConstraint), store (FileStorage), and serve (FileResponse) behind Scafera-owned types. Server-side MIME detection via magic bytes, automatic path-traversal sanitization, and collision-safe filenames.Depends on: A Scafera host project whose architecture package defines a storage directory via
getStorageDir()(e.g.var/uploads/inscafera/layered).Extension points: None of its own — the package exposes concrete services (
UploadExtractor,UploadValidator,FileStorage,FileResponse) used directly.UploadConstraintis configured per-call with allowed extensions, MIME types, and max size.Not responsible for: Form processing (compose with
scafera/formper ADR-062) · client-supplied MIME types (server-side magic bytes only) · choosing the storage directory (architecture package ownsgetStorageDir()) · direct use of Symfony file types in userland (blocked byFileBoundaryPassandFileBoundaryValidator).
This is a capability package. It adds optional file handling to a Scafera project. It does not define folder structure or architectural rules — those belong to architecture packages.
What it provides
UploadedFile— file metadata (name, extension, MIME, size) with server-side MIME detectionUploadExtractor— extract uploads from requestsUploadConstraint+UploadValidator— validate files (extension, MIME, size)FileStorage— store files with path traversal protection and collision handlingFileResponse— serve files as download or inline
Design decisions
- MIME detection is server-side — uses
symfony/mimemagic bytes, not the client-supplied Content-Type header. Prevents spoofed file types. - Path traversal is structurally prevented — directory segments are sanitized (stripped of
..and.) before any filesystem operation. No file is ever written before validation. - File collision handling — if a file with the same name exists, a unique random suffix is appended automatically. No silent overwrite.
- Storage path is architecture-owned — the storage directory is defined by
getStorageDir()on the architecture package, not hardcoded. Forscafera/layered, this isvar/uploads/. - Form and file are separate capabilities — file uploads are not handled by
scafera/form. Controllers compose both packages explicitly (ADR-062).
Installation
composer require scafera/file
Requirements
- PHP >= 8.4
- scafera/kernel
Upload and validate
use Scafera\File\UploadExtractor; use Scafera\File\UploadValidator; use Scafera\File\UploadConstraint; $file = $uploads->get($request, 'avatar'); if ($file !== null) { $result = $validator->validate($file, new UploadConstraint( allowedExtensions: ['jpg', 'png'], allowedMimeTypes: ['image/jpeg', 'image/png'], maxSizeBytes: 2_097_152, // 2 MB )); if (!$result->isValid()) { // $result->error() — human-readable message } }
MIME detection uses symfony/mime magic bytes — not the client-supplied MIME type.
Store
use Scafera\File\FileStorage; $path = $storage->store($file, 'avatars'); // 'avatars/photo.jpg' $path = $storage->store($file, 'avatars', 'me.jpg'); // 'avatars/me.jpg' $storage->exists($path); // true $storage->delete($path); // true
The storage directory is defined by the architecture package via getStorageDir(). For scafera/layered, this is var/uploads/. Filenames are sanitized (directory components stripped). If a file with the same name already exists, a unique suffix is appended automatically.
Path traversal is structurally prevented — directory segments are sanitized before any filesystem operation.
Serve files
use Scafera\File\FileResponse; return FileResponse::download($path, 'report.pdf'); return FileResponse::inline($path);
FileResponse does not implement ResponseInterface. A dedicated listener converts it to a binary response at priority 10 (before the kernel's response listener).
File uploads in forms
File uploads are not handled by scafera/form. Use both packages together in your controller (ADR-062):
$form = $this->formHandler->handle($request, ProfileInput::class); $avatar = $this->uploads->get($request, 'avatar');
Two explicit calls. Form handles POST data, file handles uploads. Each validates independently.
Boundary enforcement
| Blocked | Use instead |
|---|---|
Symfony\Component\HttpFoundation\File\UploadedFile |
Scafera\File\UploadedFile |
Symfony\Component\HttpFoundation\File\File |
Scafera\File\FileStorage |
Symfony\Component\Filesystem\* |
Scafera\File\FileStorage |
Enforced via compiler pass (build time) and validator (scafera validate). Detects use, new, and extends patterns.
License
MIT
scafera/file 适用场景与选型建议
scafera/file 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「file」 「storage」 「scafera」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 scafera/file 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 scafera/file 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 scafera/file 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A SDK for working with B2 cloud storage.
Small library to access Microsoft Windows Azure Blob Storage with a Service or a StreamWrapper.
The file manager intended for using Laravel with CKEditor / TinyMCE / Colorbox
Laravel Media Popup to upload/view the files
PMVC Plugin for File information
Mapper for accessing resources in KWCMS
统计信息
- 总下载量: 6
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 21
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-04-14