PHPackages                             changhorizon/file-uploader - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. changhorizon/file-uploader

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

changhorizon/file-uploader
==========================

A flexible, decoupled file upload library with customizable validation and scoped storage management for secure and maintainable uploads.

v0.1.2(4w ago)01MITPHPPHP &gt;=8.2CI passing

Since Sep 3Pushed 4w agoCompare

[ Source](https://github.com/changhorizon/file-uploader)[ Packagist](https://packagist.org/packages/changhorizon/file-uploader)[ RSS](/packages/changhorizon-file-uploader/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (3)Dependencies (6)Versions (5)Used By (0)

File Uploader
=============

[](#file-uploader)

> 灵活、解耦的文件上传组件

[![License](https://camo.githubusercontent.com/16fe1ef8ac60cca5743c7ceafb48f608cf88cfcc9ee1d94ab7f353741c844280/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6368616e67686f72697a6f6e2f66696c652d75706c6f616465723f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/16fe1ef8ac60cca5743c7ceafb48f608cf88cfcc9ee1d94ab7f353741c844280/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6368616e67686f72697a6f6e2f66696c652d75706c6f616465723f7374796c653d666c61742d737175617265)[![Latest Version](https://camo.githubusercontent.com/228c7eebba27af89b368cca4d02ff21573eb861fe9d44eef0bd7be520f78a4bd/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6368616e67686f72697a6f6e2f66696c652d75706c6f616465723f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/228c7eebba27af89b368cca4d02ff21573eb861fe9d44eef0bd7be520f78a4bd/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6368616e67686f72697a6f6e2f66696c652d75706c6f616465723f7374796c653d666c61742d737175617265)[![PHP Version](https://camo.githubusercontent.com/10b897c523f00fa3f8f7b54dfe73999190e622480655d5b5f011e31fc32a7111/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d382e322d2d382e342d626c75653f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/10b897c523f00fa3f8f7b54dfe73999190e622480655d5b5f011e31fc32a7111/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d382e322d2d382e342d626c75653f7374796c653d666c61742d737175617265)[![Static Analysis](https://camo.githubusercontent.com/1a477f5e7e742a33c1ff5b685167579083a26c342c4e997fe056ea4ef7bea73e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7374617469635f616e616c797369732d5048505374616e2d626c75653f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/1a477f5e7e742a33c1ff5b685167579083a26c342c4e997fe056ea4ef7bea73e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7374617469635f616e616c797369732d5048505374616e2d626c75653f7374796c653d666c61742d737175617265)[![Tests](https://camo.githubusercontent.com/72829871c802983bff15745f71c846973b36a6fea0bb5298dadaeeb690463604/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f74657374732d504850556e69742d627269676874677265656e3f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/72829871c802983bff15745f71c846973b36a6fea0bb5298dadaeeb690463604/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f74657374732d504850556e69742d627269676874677265656e3f7374796c653d666c61742d737175617265)[![codecov](https://camo.githubusercontent.com/0cd7dc9af552a59c728b8a6e3bbe95614ea7f42b896605fb13bf7a0411297b8d/68747470733a2f2f636f6465636f762e696f2f67682f6368616e67686f72697a6f6e2f66696c652d75706c6f616465722f6272616e63682f6d61696e2f67726170682f62616467652e737667)](https://codecov.io/gh/changhorizon/file-uploader)[![CI](https://github.com/changhorizon/file-uploader/actions/workflows/ci.yml/badge.svg?style=flat-square)](https://github.com/changhorizon/file-uploader/actions/workflows/ci.yml/badge.svg?style=flat-square)

一个轻量组件，支持自定义验证和作用域标识管理（ScopedStorageStrategy），适合有状态或无状态的 HTTP API 场景，可快速集成到现有系统。

✨ 特性
----

[](#-特性)

- 自定义文件验证：通过 `UploadedFileValidator` 支持扩展名、大小等规则
- 作用域标识管理：支持 `ScopedStorageStrategy` 管理文件作用域或会话信息
- 面向接口编程：核心接口 `FileUploaderInterface`、`UploadContextInterface`、`UploadedFileInterface` 易于扩展
- 轻量易用：独立组件，可快速集成到现有系统，无需依赖框架

📦 安装
----

[](#-安装)

```
composer require changhorizon/file-uploader
```

📂 目录结构
------

[](#-目录结构)

```
src
├── Exception
│   ├── FileUploadEmptyException.php
│   └── FileUploadException.php
├── Local
│   ├── FileUploader.php
│   ├── UploadContext.php
│   ├── UploadedFile.php
│   └── UploadedFileResult.php
├── Validator
│   └── UploadedFileValidator.php
├── FileUploaderInterface.php
├── UploadContextInterface.php
├── UploadedFileInterface.php
└── UploadedFileResultInterface.php
```

🚀 用法示例
------

[](#-用法示例)

### 示例 1：单文件上传

[](#示例-1单文件上传)

```
use ChangHorizon\FileUploader\Local\FileUploader;
use ChangHorizon\FileUploader\Local\UploadContext;
use ChangHorizon\FileUploader\Local\UploadedFile;
use ChangHorizon\FileUploader\Validator\UploadedFileValidator;

$file = new UploadedFile('example.png', 'image/png', '/tmp/php123', 102400);

$context = new UploadContext(
    basePath: '/var/www/uploads',
    validator: new UploadedFileValidator()
);

$uploader = new FileUploader($context);
$result = $uploader->upload($file);

if ($result->isValid()) {
    echo "File uploaded successfully: " . $result->getPath();
} else {
    echo "Upload failed: " . $result->getError();
}
```

### 示例 2：多文件上传

[](#示例-2多文件上传)

```
$files = [
    new UploadedFile('a.png', 'image/png', '/tmp/phpA', 102400),
    new UploadedFile('b.pdf', 'application/pdf', '/tmp/phpB', 204800),
];

$results = [];
foreach ($files as $file) {
    $results[] = $uploader->upload($file);
}

foreach ($results as $result) {
    if ($result->isValid()) {
        echo "Uploaded: " . $result->getPath() . PHP_EOL;
    } else {
        echo "Failed: " . $result->getError() . PHP_EOL;
    }
}
```

📐 接口说明
------

[](#-接口说明)

### FileUploaderInterface

[](#fileuploaderinterface)

> 文件上传核心接口，提供单文件和批量上传方法

```
interface FileUploaderInterface
{
    public function upload(UploadedFileInterface $file): UploadedFileResultInterface;
}
```

### UploadedFileValidator

[](#uploadedfilevalidator)

> 文件验证器，可自定义允许的扩展名和大小限制

```
class UploadedFileValidator
{
    public function __construct(array $allowedExtensions = ['jpg','png','gif','pdf'], int $maxSize = 5_000_000) { ... }
    public function validate(UploadedFileInterface $file): ValidationResult { ... }
}
```

🔍 静态分析
------

[](#-静态分析)

使用 PHPStan 工具进行静态分析，确保代码的质量和一致性：

```
composer stan
```

🎯 代码风格
------

[](#-代码风格)

使用 PHP-CS-Fixer 工具检查代码风格：

```
composer cs:chk
```

使用 PHP-CS-Fixer 工具自动修复代码风格问题：

```
composer cs:fix
```

✅ 单元测试
------

[](#-单元测试)

执行 PHPUnit 单元测试：

```
composer test
```

执行 PHPUnit 单元测试并生成代码覆盖率报告：

```
composer test:coverage
```

🤝 贡献指南
------

[](#-贡献指南)

欢迎 Issue 与 PR，建议遵循以下流程：

1. Fork 仓库
2. 创建新分支进行开发
3. 提交 PR 前请确保测试通过、风格一致
4. 提交详细描述

📜 License
---------

[](#-license)

MIT License. See the [LICENSE](LICENSE) file for details.

###  Health Score

38

—

LowBetter than 83% of packages

Maintenance94

Actively maintained with recent releases

Popularity2

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity42

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~83 days

Total

4

Last Release

29d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/191742660?v=4)[ChangHorizon](/maintainers/changhorizon)[@changhorizon](https://github.com/changhorizon)

---

Top Contributors

[![changhorizon](https://avatars.githubusercontent.com/u/191742660?v=4)](https://github.com/changhorizon "changhorizon (8 commits)")

---

Tags

validatorfile-uploaduploaderscoped-check

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/changhorizon-file-uploader/health.svg)

```
[![Health](https://phpackages.com/badges/changhorizon-file-uploader/health.svg)](https://phpackages.com/packages/changhorizon-file-uploader)
```

###  Alternatives

[respect/validation

The most awesome validation engine ever created for PHP

6.1k39.0M405](/packages/respect-validation)[seld/jsonlint

JSON Linter

1.3k224.9M252](/packages/seld-jsonlint)[composer/spdx-licenses

SPDX licenses list and validation library.

1.4k190.2M34](/packages/composer-spdx-licenses)[opis/json-schema

Json Schema Validator for PHP

64841.2M257](/packages/opis-json-schema)[intervention/validation

Additional validation rules for the Laravel framework

6777.1M18](/packages/intervention-validation)[laminas/laminas-validator

Validation classes for a wide range of domains, and the ability to chain validators to create complex validation criteria

15847.1M212](/packages/laminas-laminas-validator)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
