承接 scafera/file 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

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 (UploadExtractorUploadedFile), 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/ in scafera/layered).

Extension points: None of its own — the package exposes concrete services (UploadExtractor, UploadValidator, FileStorage, FileResponse) used directly. UploadConstraint is configured per-call with allowed extensions, MIME types, and max size.

Not responsible for: Form processing (compose with scafera/form per ADR-062) · client-supplied MIME types (server-side magic bytes only) · choosing the storage directory (architecture package owns getStorageDir()) · direct use of Symfony file types in userland (blocked by FileBoundaryPass and FileBoundaryValidator).

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 detection
  • UploadExtractor — extract uploads from requests
  • UploadConstraint + UploadValidator — validate files (extension, MIME, size)
  • FileStorage — store files with path traversal protection and collision handling
  • FileResponse — serve files as download or inline

Design decisions

  • MIME detection is server-side — uses symfony/mime magic 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. For scafera/layered, this is var/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 我们能提供哪些服务?
定制开发 / 二次开发

基于 scafera/file 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-14