initphp/upload
Composer 安装命令:
composer require initphp/upload
包简介
Validate and store uploaded files on local disk, FTP/FTPS or Amazon S3 through a single adapter-based API.
README 文档
README
Validate and store uploaded files on local disk, FTP/FTPS or Amazon S3 through a single, adapter-based API.
Requirements
- PHP 8.0 or higher
ext-fileinfo(used to detect the real MIME type of a file)ext-ftp— only for the FTP adapteraws/aws-sdk-php— only for the S3 adapter
Installation
composer require initphp/upload
For S3 uploads, also install the AWS SDK:
composer require aws/aws-sdk-php
Quick start
require 'vendor/autoload.php'; use InitPHP\Upload\Upload; use InitPHP\Upload\File; use InitPHP\Upload\Adapters\LocalAdapter; $adapter = new LocalAdapter([ 'dir' => __DIR__ . '/uploads/', 'url' => 'https://example.com/uploads/', ], [ 'allowed_extensions' => ['jpg', 'jpeg', 'png', 'webp'], 'allowed_max_size' => 2 * 1024 * 1024, // 2 MB, in bytes ]); $upload = new Upload($adapter); foreach (File::setPost('photos') as $file) { $stored = $upload->setFile($file)->to(); if ($stored !== false) { echo 'Uploaded to: ' . $stored->getURL(); } }
File::setPost('photos') reads $_FILES['photos'] and returns a normalized
File[], whether the field uploaded one file or many. to() validates the
file, stores it, and returns the stored File (with its public URL set) or
false if the underlying write failed. Any validation or transfer problem is
thrown as an UploadException.
Loading files
| Call | Returns | Purpose |
|---|---|---|
File::setPost(string $key) |
File[] |
Normalize $_FILES[$key] (single or multiple) into File objects. |
File::setPath(string $path) |
File |
Wrap an existing file already on disk. |
A file loaded with setPath() is copied to its destination (the source is
left in place); a real HTTP upload is moved with move_uploaded_file().
Adapters
All three adapters share the same API — only the credentials differ. The optional second constructor argument is the validation options, identical across adapters.
| Adapter | Credentials | Notes |
|---|---|---|
LocalAdapter |
dir, url |
Creates missing destination directories. |
FTPAdapter |
host, port, username, password, timeout, url, passive, ssl |
Binary mode, passive by default, optional FTPS. |
S3Adapter |
key, secret_key, region, bucket, ACL, version |
Requires aws/aws-sdk-php. |
The to($target) argument
$target means the same thing in every adapter: a destination path/key
prefix. to('avatars/2026') stores the file under that sub-path:
- Local →
<dir>/avatars/2026/<name> - FTP →
<remote>/avatars/2026/<name>(directories created as needed) - S3 → object key
avatars/2026/<name>in the configured bucket
Call to() with no argument to store the file at the destination root.
Validation options
Pass these as the second constructor argument of any adapter (an empty array or
0 means "no restriction"):
| Option | Type | Meaning |
|---|---|---|
allowed_extensions |
string[] |
Allowed file extensions, matched case-insensitively against the original file name. |
allowed_mime_types |
string[] |
Allowed MIME types, matched against the real type detected with finfo (not the client-supplied one). |
allowed_max_size |
int |
Maximum size in bytes. |
A file that violates any restriction — or whose upload errored — makes to()
throw an UploadException.
Renaming
$file->rename('profile'); // keeps the original extension $upload->setFile($file)->to(); // stored as profile.jpg
Documentation
Full guides with examples live in docs/:
Contributing
Bug reports and pull requests are welcome. CI runs PHP-CS-Fixer, PHPStan (max level) and PHPUnit across PHP 8.0–8.4; run the same bundle locally with:
composer ci
Credits
License
Copyright © 2023 InitPHP — released under the MIT License.
initphp/upload 适用场景与选型建议
initphp/upload 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 39 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 02 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「filesystem」 「s3」 「upload」 「adapter」 「aws」 「ftp」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 initphp/upload 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 initphp/upload 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 initphp/upload 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Adapter designed to add Framework Agnosticism for Caching within PHP Packages.
A SDK for working with B2 cloud storage.
A PHP class providing static methods for reading, writing, copying, moving, and deleting files and directories, MIME type detection, image size detection, and file permission management
flysystem cache Adapter
Virtual Filesystem Storage Adapter for Laravel
The file manager intended for using Laravel with CKEditor / TinyMCE / Colorbox
统计信息
- 总下载量: 39
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-02-19